AggregateSaasQrcodeForm.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\AggregateSaasQrcode as 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 AggregateSaasQrcodeForm extends Model
  24. {
  25. public $id;
  26. public $store_id;
  27. public $param_url;
  28. public $admin_id;
  29. public $is_use;
  30. public $mini_url;
  31. public $create_time;
  32. public $number;
  33. public $name;
  34. public $salesman_name;
  35. public $status;
  36. public $salesman_id;
  37. public $ids;
  38. public function rules()
  39. {
  40. return [
  41. [["id", "store_id", "mini_id", "admin_id", "salesman_id", "is_use", "number", 'salesman_id'], 'integer'],
  42. [["param_url", "qrcode_url", "mini_url", "create_time",], 'string'],
  43. [['ids'], 'array']
  44. ];
  45. }
  46. public function createdUrl()
  47. {
  48. $num = 0;
  49. $id = [];
  50. foreach (range(1, $this->number) as $item) {
  51. $max_id = AggregateQrcode::find()->max('id');
  52. $qrcode = new AggregateQrcode();
  53. $qrcode->create_time = time();
  54. $qrcode->store_id = 0;
  55. $qrcode->mini_url = "/face-pay/face-pay/payOut";
  56. $res = $qrcode->save();
  57. if ($res) {
  58. $qrcode->param_url = \Yii::$app->request->hostInfo . '/web/face/saas/pay?id=' . ($qrcode->id);
  59. $res = $qrcode->save();
  60. if ($res) {
  61. array_push($id, $qrcode->id);
  62. $num++;
  63. }
  64. }
  65. }
  66. return [
  67. 'code' => 0,
  68. 'msg' => '已成功生成' . $num . '条链接',
  69. 'id' => $id
  70. ];
  71. }
  72. public function qrcodeList()
  73. {
  74. //业务员列表
  75. $query = Salesman::find()->alias('s')->leftJoin(['su' => SaasUser::tableName()], 's.saas_user_id = su.id')->where(['s.is_delete' => 0]);
  76. $Salesman = $query->select('s.id, su.name')->asArray()->all();
  77. //end
  78. //商城列表,排除已经绑定过聚合码的商城 start
  79. $store_ids = AggregateQrcode::find()->where(['and', ['<>', 'store_id', 0]])->select("store_id")->column();
  80. if (\Yii::$app->prod_is_dandianpu()) {
  81. $self_mini = Option::find()->where(['name' => 'self_mini', 'group' => 'store', 'value' => 1])->select("store_id")->column();
  82. $store_ids = array_merge($self_mini, $store_ids);
  83. $stores = Store::find()->where(['not in', 'id', $store_ids])->andWhere(['is_delete' => 0])->asArray()->select('id, name, salesman_id')->all();
  84. } else {
  85. $stores = Store::find()->where(['not in', 'id', $store_ids])->andWhere(['and', ['is_delete' => 0], ['<>', 'business_model', '1']])->asArray()->select('id, name, salesman_id')->all();
  86. }
  87. //end
  88. $query = AggregateQrcode::find()->alias('aq')->leftJoin(['s' => Store::tableName()], 'aq.store_id = s.id');
  89. $query->leftJoin(['sm' => Salesman::tableName()], 'aq.salesman_id = sm.id');
  90. $query->leftJoin(['su' => SaasUser::tableName()], 'sm.saas_user_id = su.id');
  91. $query->leftJoin(['a' => Admin::tableName()], 'aq.admin_id = a.id');
  92. $query->leftJoin(['c' => SaasCategory::tableName()], 's.category_id = c.id');
  93. if (!empty($this->name)) {
  94. $query->andWhere(['LIKE', 's.name', $this->name]);
  95. }
  96. if (!empty($this->salesman_name)) {
  97. $query->andWhere(['LIKE', 'sm.name', $this->salesman_name]);
  98. }
  99. switch ($this->status) {
  100. case 1:
  101. $query->andWhere(['shop_bind_time' => '']);
  102. break;
  103. case 2:
  104. $query->andWhere(['<>', 'shop_bind_time', '']);
  105. break;
  106. }
  107. $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, su.name salesman_name, a.name admin_name, s.logo as store_logo, c.name as category_name')
  108. ->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. $item['business_no'] = null;
  118. $wechatConfig = WechatConfig::find()->where(['store_id' => $item['store_id']])->select('mch_id')->one();
  119. if ($wechatConfig) {
  120. $item['business_no'] = $wechatConfig->mch_id;
  121. }
  122. }
  123. return [
  124. 'code' => 0,
  125. 'msg' => "获取成功",
  126. 'data' => $pagination['list'],
  127. 'store_list' => $stores,//商城列表
  128. 'salesman_list' => $Salesman,
  129. 'pageNo' => $pagination['pageNo'],
  130. 'totalCount' => $pagination['totalCount']
  131. ];
  132. }
  133. public function urlDel()
  134. {
  135. $t = \Yii::$app->db->beginTransaction();
  136. try {
  137. if (empty($this->ids)) {
  138. throw new \Exception("没有选择数据");
  139. }
  140. $res = AggregateQrcode::deleteAll(['id' => $this->ids]);
  141. if ($res) {
  142. $t->commit();
  143. return [
  144. 'code' => 0,
  145. 'msg' => "删除成功"
  146. ];
  147. } else {
  148. throw new \Exception("删除失败");
  149. }
  150. } catch (\Exception $e) {
  151. $t->rollBack();
  152. return [
  153. 'code' => 0,
  154. 'msg' => $e->getMessage()
  155. ];
  156. }
  157. }
  158. //绑定商城
  159. public function bindStore()
  160. {
  161. try {
  162. $ag = AggregateQrcode::findone($this->id);
  163. if (!empty($ag)) {
  164. //绑定商城
  165. if (!empty($this->store_id)) {
  166. $st_ag = AggregateQrcode::findone(['store_id' => $this->store_id]);
  167. if (!empty($st_ag)) {
  168. throw new \Exception("此商城已经被绑定");
  169. }
  170. if (!empty($ag->store_id)) {
  171. throw new \Exception("已经绑定过商城");
  172. }
  173. $store = Store::find()->where(['id' => $this->store_id])->select("id,salesman_id")->one();
  174. if (!empty($store->salesman_id)) {
  175. $Salesman = Salesman::findOne($store->salesman_id);
  176. $ag->salesman_id = $store->salesman_id;
  177. $ag->admin_id = $Salesman->admin_id;
  178. }
  179. $ag->store_id = $this->store_id;
  180. $ag->shop_bind_time = time();
  181. if (!$ag->save()) {
  182. throw new \Exception("商城绑定失败");
  183. }
  184. return [
  185. 'code' => 0,
  186. 'msg' => "绑定成功"
  187. ];
  188. }
  189. if (empty($ag->store_id)) {
  190. throw new \Exception("请先绑定商城");
  191. }
  192. } else {
  193. throw new \Exception("没有找到二维码信息");
  194. }
  195. } catch (\Exception $e) {
  196. return [
  197. 'code' => 1,
  198. 'msg' => $e->getMessage()
  199. ];
  200. }
  201. }
  202. //生成可使用的链接进行显示
  203. public function showQrcode()
  204. {
  205. try {
  206. $qrcode = AggregateQrcode::find()->where(['store_id' => 0, 'is_use' => 1])->andWhere(['and', ['<>', 'param_url', ''], ['<>', 'mini_url', '']])->select('id, mini_url, param_url')->one();
  207. if (empty($qrcode)) {
  208. $this->number = 1;
  209. $result = $this->createdUrl();
  210. $qrcode = AggregateQrcode::find()->where(['id' => $result['id']])->select('id, mini_url, param_url')->one();
  211. }
  212. $qrcode->mini_url = substr($qrcode->mini_url, 1, strlen($qrcode->mini_url));
  213. if (!empty($qrcode->param_url)) {
  214. $index = strpos($qrcode->param_url, '?');
  215. $qrcode->param_url = substr($qrcode->param_url, 0, $index);
  216. $index = strpos($qrcode->param_url, '//');
  217. $qrcode->param_url = substr($qrcode->param_url, ($index + 2));
  218. }
  219. return [
  220. 'code' => 0,
  221. 'msg' => "获取成功",
  222. 'data' => $qrcode
  223. ];
  224. } catch (\Exception $e) {
  225. return [
  226. 'code' => 1,
  227. 'msg' => $e->getMessage()
  228. ];
  229. }
  230. }
  231. }