AggregateQrcodeForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Admin;
  9. use app\models\AggregateQrcode;
  10. use app\models\Option;
  11. use app\models\SaasUser;
  12. use app\models\Salesman;
  13. use app\models\Store;
  14. use app\models\StoreAliMini;
  15. use app\models\StoreMini;
  16. use app\models\WechatConfig;
  17. use app\utils\QrCode;
  18. use EasyWeChat\Factory;
  19. use yii\base\Model;
  20. use AopClient;
  21. use app\models\SaasCategory;
  22. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  23. class AggregateQrcodeForm extends Model
  24. {
  25. public $id;
  26. public $store_id;
  27. public $param_url;
  28. public $admin_id;
  29. public $is_use;
  30. public $wx_url;
  31. public $ali_url;
  32. public $create_time;
  33. public $number;
  34. public $name;
  35. public $salesman_name;
  36. public $status;
  37. public $salesman_id;
  38. public $ids;
  39. public $type;
  40. public function rules()
  41. {
  42. return [
  43. [["id", "store_id", "mini_id", "admin_id", "salesman_id", "is_use", "number",'salesman_id', 'type'],'integer'],
  44. [["param_url", "qrcode_url", "wx_url", "ali_url", "create_time",],'string'],
  45. [['ids'],'array']
  46. ];
  47. }
  48. public function createdUrl(){
  49. $num = 0;
  50. $id = [];
  51. foreach(range(1,$this->number) as $item){
  52. $max_id = AggregateQrcode::find()->max('id');
  53. $qrcode = new AggregateQrcode();
  54. $qrcode->create_time = time();
  55. $qrcode->store_id = 0;
  56. $qrcode->param_url = \Yii::$app->request->hostInfo.'/web/face/pay/'.($max_id+1);
  57. $qrcode->wx_url = "/face-pay/face-pay/payOut";
  58. $qrcode->ali_url = "/face-pay/face-pay/payOut";
  59. $res = $qrcode->save();
  60. if($res){
  61. $qrcode->param_url = \Yii::$app->request->hostInfo.'/web/face/pay/'.($qrcode->id);
  62. $qrcode->save();
  63. array_push($id, $qrcode->id);
  64. $num++;
  65. }
  66. }
  67. return [
  68. 'code'=>0,
  69. 'msg'=>'已成功生成'.$num.'条链接'
  70. ];
  71. }
  72. public function qrcodeList(){
  73. //业务员列表
  74. $query = Salesman::find()->alias('s')->leftJoin(['su'=>SaasUser::tableName()],'s.saas_user_id = su.id')->where(['s.is_delete'=>0]);
  75. $Salesman = $query->select('s.id,su.name')->asArray()->all();
  76. //end
  77. //商城列表,排除已经绑定过聚合码的商城 start
  78. $store_ids = AggregateQrcode::find()->where(['and',['<>','store_id',0]])->select("store_id")->column();
  79. if (\Yii::$app->prod_is_dandianpu()) {
  80. $self_mini = Option::find()->where(['name' => 'self_mini', 'group' => 'store', 'value' => 1])->select("store_id")->column();
  81. $stores = Store::find()->where(['not in','id',$store_ids])->andWhere(['is_delete' => 0, 'id' => $self_mini])->asArray()->select('id,name,salesman_id')->all();
  82. } else {
  83. $stores = Store::find()->where(['not in','id',$store_ids])->andWhere(['is_delete' => 0, 'business_model' => 1])->asArray()->select('id,name,salesman_id')->all();
  84. }
  85. //end
  86. $query = AggregateQrcode::find()->alias('aq')->leftJoin(['s'=>Store::tableName()],'aq.store_id = s.id');
  87. $query->leftJoin(['sm'=>Salesman::tableName()],'aq.salesman_id = sm.id');
  88. $query->leftJoin(['su'=>SaasUser::tableName()],'sm.saas_user_id = su.id');
  89. $query->leftJoin(['a'=>Admin::tableName()],'aq.admin_id = a.id');
  90. $query->leftJoin(['c' => SaasCategory::tableName()], 's.category_id = c.id');
  91. if(!empty($this->name)){
  92. $query->andWhere(['LIKE','s.name',$this->name]);
  93. }
  94. if(!empty($this->salesman_name)){
  95. $query->andWhere(['LIKE','sm.name',$this->salesman_name]);
  96. }
  97. switch ($this->status){
  98. case 1:
  99. $query->andWhere(['shop_bind_time'=>'']);
  100. break;
  101. case 2:
  102. $query->andWhere(['<>','shop_bind_time','']);
  103. break;
  104. case 3:
  105. $query->andWhere(['and',['<>','shop_bind_time',''],['<>','mini_time','']]);
  106. break;
  107. }
  108. $query = $query->select('s.name store_name,aq.id,aq.store_id,aq.admin_id,aq.salesman_id,aq.is_use,aq.shop_bind_time,aq.mini_time,su.name salesman_name,a.name admin_name,s.logo as store_logo,c.name as category_name, aq.wx_status, aq.ali_status')->orderBy('id desc');
  109. $pagination = pagination_make($query);
  110. foreach($pagination['list'] as &$item){
  111. if(empty($item['shop_bind_time'])){
  112. $item['status']=1;
  113. }
  114. if(!empty($item['shop_bind_time'])){
  115. $item['status']=2;
  116. }
  117. if(!empty($item['shop_bind_time']) && !empty($item['mini_time'])){
  118. $item['status']=3;
  119. }
  120. $item['business_no'] = null;
  121. $item['wx_status'] *= 1;
  122. $item['ali_status'] *= 1;
  123. $wechatConfig = WechatConfig::find()->where(['store_id' => $item['store_id']])->select('mch_id')->one();
  124. if ($wechatConfig) {
  125. $item['business_no'] = $wechatConfig->mch_id;
  126. }
  127. }
  128. return [
  129. 'code'=>0,
  130. 'msg' =>"获取成功",
  131. 'data' => $pagination['list'],
  132. 'store_list'=>$stores,//商城列表
  133. 'salesman_list'=>$Salesman,
  134. 'pageNo' => $pagination['pageNo'],
  135. 'totalCount' => $pagination['totalCount']
  136. ];
  137. }
  138. public function urlDel(){
  139. $t = \Yii::$app->db->beginTransaction();
  140. try {
  141. if(empty($this->ids)){
  142. throw new \Exception("没有选择数据");
  143. }
  144. $res = AggregateQrcode::deleteAll(['id'=>$this->ids]);
  145. if($res){
  146. $t->commit();
  147. return [
  148. 'code'=>0,
  149. 'msg'=>"删除成功"
  150. ];
  151. }else{
  152. throw new \Exception("删除失败");
  153. }
  154. }catch (\Exception $e){
  155. $t->rollBack();
  156. return [
  157. 'code'=>0,
  158. 'msg'=>$e->getMessage()
  159. ];
  160. }
  161. }
  162. //绑定商城
  163. public function bindStore(){
  164. try {
  165. $ag = AggregateQrcode::findone($this->id);
  166. if(!empty($ag)){
  167. //绑定商城
  168. if(!empty($this->store_id)){
  169. $st_ag = AggregateQrcode::findone(['store_id' => $this->store_id]);
  170. if(!empty($st_ag)){
  171. throw new \Exception("此商城已经被绑定");
  172. }
  173. if(!empty($ag->store_id)){
  174. throw new \Exception("已经绑定过商城");
  175. }
  176. $store = Store::find()->where(['id'=>$this->store_id])->select("id,salesman_id")->one();
  177. if(!empty($store->salesman_id)){
  178. $Salesman = Salesman::findOne($store->salesman_id);
  179. $ag->salesman_id = $store->salesman_id;
  180. $ag->admin_id = $Salesman->admin_id;
  181. }
  182. $ag->store_id = $this->store_id;
  183. $ag->shop_bind_time = time();
  184. if(!$ag->save()){
  185. throw new \Exception("商城绑定失败");
  186. }
  187. return [
  188. 'code' => 0,
  189. 'msg' => "绑定成功"
  190. ];
  191. }
  192. if(empty($ag->store_id)){
  193. throw new \Exception("请先绑定商城");
  194. }
  195. throw new \Exception("没有传入任何可用参数");
  196. }else{
  197. throw new \Exception("没有找到二维码信息");
  198. }
  199. }catch (\Exception $e){
  200. return [
  201. 'code'=>1,
  202. 'msg'=>$e->getMessage()
  203. ];
  204. }
  205. }
  206. public function qrcodeBindInfo(){
  207. try {
  208. $aq = AggregateQrcode::findOne(['store_id'=>get_store_id()]);
  209. $w_mini = StoreMini::find()->where(['store_id'=>get_store_id()])->select("id,appid")->orderBy('id desc')->one();
  210. $ali_mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->select("id,auth_app_id")->orderBy('id desc')->one();
  211. if(empty($aq)){
  212. return [
  213. 'code'=>1,
  214. 'msg'=>"未查找到任何聚合码信息"
  215. ];
  216. }
  217. $data['wx_status'] = empty($w_mini->appid)?'-1':$aq->wx_status;
  218. $data['ali_status'] = empty($ali_mini->auth_app_id)?'-1':$aq->ali_status;
  219. return [
  220. 'code'=>0,
  221. 'msg'=>"查询成功",
  222. 'data'=>$data
  223. ];
  224. }catch (\Exception $e){
  225. return [
  226. 'code'=>1,
  227. 'msg'=>$e->getMessage()
  228. ];
  229. }
  230. }
  231. //绑定普通二维码
  232. public function bindQrcode()
  233. {
  234. try {
  235. $type = (int)$this->type;
  236. $id = $this->id;
  237. $qrcode = AggregateQrcode::findOne($id);
  238. if (empty($qrcode)) {
  239. throw new \Exception("未找到聚合码信息");
  240. }
  241. if (empty($qrcode->store_id)) {
  242. throw new \Exception("请绑定商城信息");
  243. }
  244. //如果是微信方式
  245. if ($type === 1) {
  246. //判断是否已经绑定微信 但二维码未绑定
  247. if ((int)$qrcode->wx_status === 0) {
  248. if (!empty($qrcode->wx_mini_id)) {
  249. $store_mini = StoreMini::findOne($qrcode->wx_mini_id);
  250. } else {
  251. $store_mini = StoreMini::findOne(['store_id' => $qrcode->store_id, 'is_cancle' => 0, 'is_use' => 1]);
  252. if (empty($store_mini)) {
  253. throw new \Exception("需要使用三方注册小程序或查看小程序是否已经上线使用");
  254. }
  255. $qrcode->wx_mini_id = $store_mini->id;
  256. if (!$qrcode->save()) {
  257. throw new \Exception(json_encode($qrcode->errors));
  258. }
  259. }
  260. if (empty($store_mini)) {
  261. throw new \Exception("需要使用三方注册小程序");
  262. }
  263. $form = new WechatThirdForm();
  264. $form->bind_store_id = $qrcode->store_id;
  265. $result = $form->downQrcodeRules($store_mini->id, $store_mini->appid);
  266. if ($result['code'] !== 0) {
  267. throw new \Exception($result['msg']);
  268. }
  269. return $result;
  270. } else {
  271. throw new \Exception("已经绑定聚合码信息");
  272. }
  273. } else {
  274. if ((int)$qrcode->ali_status === 0) {
  275. if (!empty($qrcode->ali_mini_id)) {
  276. $store_mini = StoreAliMini::findOne(['id' => $qrcode->ali_mini_id, 'is_cancel' => 0]);
  277. } else {
  278. $store_mini = StoreAliMini::findOne(['store_id' => $qrcode->store_id, 'is_cancel' => 0, 'is_use' => 1]);
  279. if (empty($store_mini)) {
  280. throw new \Exception("需要使用三方注册小程序或查看小程序是否已经上线使用");
  281. }
  282. $qrcode->ali_mini_id = $store_mini->id;
  283. if (!$qrcode->save()) {
  284. throw new \Exception(json_encode($qrcode->errors));
  285. }
  286. }
  287. if (empty($store_mini)) {
  288. throw new \Exception("需要使用三方注册小程序");
  289. }
  290. $form = new AlipayThirdForm();
  291. $result = $form->bindQrcode($store_mini->id);
  292. if ($result['code'] !== 0) {
  293. throw new \Exception($result['msg']);
  294. }
  295. return [
  296. 'code' => 0,
  297. 'msg' => '操作成功',
  298. 'data' => $result
  299. ];
  300. } else {
  301. throw new \Exception("已经绑定聚合码信息");
  302. }
  303. }
  304. } catch (\Exception $e) {
  305. return [
  306. 'code' => 1,
  307. 'msg' => $e->getMessage()
  308. ];
  309. }
  310. }
  311. }