CardForm.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\wechat_mp;
  8. use app\models\User;
  9. use app\models\AccountLog;
  10. use app\models\WxCard;
  11. use app\models\WxCardBind;
  12. use app\jobs\WxBonusJob;
  13. use app\modules\admin\models\WechatConfigForm;
  14. use app\models\Store;
  15. class CardForm extends Model
  16. {
  17. public $cardModel;
  18. public $bindModel;
  19. public function init() {
  20. parent::init();
  21. $this->cardModel = new WxCard();
  22. $this->bindModel = new WxCardBind();
  23. }
  24. private function card_get($card_id)
  25. {
  26. $api = $this->wechat_mp->card;
  27. $res = $api->httpPostJson('/card/get', [
  28. "card_id" => $card_id,
  29. ]);
  30. /**
  31. * {
  32. "errcode": 0,
  33. "errmsg": "ok",
  34. "card": {
  35. "card_type": "MEMBER_CARD",
  36. "member_card": {
  37. "base_info": {
  38. "id": "pBolB1m7MANGetesAPo8LpdJVeYk",
  39. "logo_url": "http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZ/0",
  40. "code_type": "CODE_TYPE_TEXT",
  41. "brand_name": "海底捞",
  42. "title": "海底捞会员卡3",
  43. "date_info": {
  44. "type": "DATE_TYPE_PERMANENT"
  45. },
  46. "color": "#63b359",
  47. "notice": "使用时向服务员出示此券",
  48. "service_phone": "020-88888888",
  49. "description": "不可与其他优惠同享",
  50. "location_id_list": [],
  51. "get_limit": 3,
  52. "can_give_friend": true,
  53. "use_custom_code": false,
  54. "status": "CARD_STATUS_VERIFY_OK",
  55. "sku": {
  56. "quantity": 49999999,
  57. "total_quantity": 50000000
  58. },
  59. "create_time": 1661329877,
  60. "update_time": 1661329908,
  61. "custom_url_name": "立即使用",
  62. "custom_url": "http://weixin.qq.com",
  63. "custom_url_sub_title": "6个汉字tips",
  64. "promotion_url": "http://www.qq.com",
  65. "promotion_url_name": "营销入口1",
  66. "need_push_on_view": true,
  67. "area_code_list": []
  68. },
  69. "supply_bonus": true,
  70. "supply_balance": false,
  71. "prerogative": "test_prerogative",
  72. "activate_url": "http://www.qq.com",
  73. "discount": 10,
  74. "custom_cell1": {
  75. "name": "使用入口2",
  76. "tips": "激活后显示",
  77. "url": "http://www.qq.com"
  78. },
  79. "auto_activate": false,
  80. "wx_activate": true,
  81. "custom_field1": {
  82. "name_type": "FIELD_NAME_TYPE_LEVEL",
  83. "url": "http://www.qq.com"
  84. },
  85. "bonus_rule": {
  86. "cost_money_unit": 100,
  87. "increase_bonus": 1,
  88. "max_increase_bonus": 200,
  89. "init_increase_bonus": 10,
  90. "cost_bonus_unit": 5,
  91. "reduce_money": 100,
  92. "least_money_to_use_bonus": 1000,
  93. "max_reduce_bonus": 50
  94. },
  95. "background_pic_url": "https://mmbiz.qlogo.cn/mmbiz/",
  96. "wx_activate_after_submit": false,
  97. "advanced_info": {
  98. "time_limit": [
  99. {
  100. "type": "MONDAY",
  101. "begin_hour": 0,
  102. "begin_minute": 10,
  103. "end_hour": 10,
  104. "end_minute": 59
  105. },
  106. {
  107. "type": "HOLIDAY"
  108. }
  109. ],
  110. "text_image_list": [
  111. {
  112. "image_url": "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
  113. "text": "此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味蕾"
  114. },
  115. {
  116. "image_url": "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sj piby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
  117. "text": "此菜品迎合大众口味,老少皆宜,营养均衡"
  118. }
  119. ],
  120. "business_service": [
  121. "BIZ_SERVICE_FREE_WIFI",
  122. "BIZ_SERVICE_WITH_PET",
  123. "BIZ_SERVICE_FREE_PARK",
  124. "BIZ_SERVICE_DELIVER"
  125. ],
  126. "consume_share_card_list": [],
  127. "abstract": {
  128. "abstract": "微信餐厅推出多种新季菜品,期待您的光临",
  129. "icon_url_list": [
  130. "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0"
  131. ]
  132. },
  133. "use_condition": {
  134. "accept_category": "鞋类",
  135. "reject_category": "阿迪达斯",
  136. "can_use_with_other_discount": true
  137. },
  138. "share_friends": false
  139. }
  140. }
  141. }
  142. }
  143. */
  144. \Yii::error([__METHOD__, $card_id, $res]);
  145. return $res;
  146. }
  147. private function card_membercard_userinfo_get($code, $card_id)
  148. {
  149. $api = $this->wechat_mp->card;
  150. $res = $api->httpPostJson('/card/membercard/userinfo/get', [
  151. "card_id" => $card_id,
  152. "code" => $code,
  153. ]);
  154. /**
  155. * {
  156. "errcode": 0,
  157. "errmsg": "ok",
  158. "openid": "oBolB1pon-jHiNVRZ8fgD8NMfxHg",
  159. "nickname": "xxx",
  160. "membership_number": "916992940983",
  161. "bonus": 3000,
  162. "sex": "MALE",
  163. "user_info": {
  164. "common_field_list": [
  165. {
  166. "name": "USER_FORM_INFO_FLAG_MOBILE",
  167. "value": "12111111111",
  168. "value_list": []
  169. }
  170. ],
  171. "custom_field_list": []
  172. },
  173. "user_card_status": "NORMAL",
  174. "has_active": true
  175. }
  176. */
  177. \Yii::error([__METHOD__, $code, $card_id, $res]);
  178. return $res;
  179. }
  180. private function card_membercard_updateuser($param)
  181. {
  182. $json = '{
  183. "code": "555075127057",
  184. "card_id": "pBolB1m7MANGetesAPo8LpdJVeYk",
  185. "record_bonus": "消费30元,获得3积分",
  186. "bonus": 3000,
  187. "add_bonus": 30,
  188. "notify_optional": {
  189. "is_notify_bonus": true,
  190. }
  191. }';
  192. $icard = json_decode($json, true);
  193. $icard['code'] = $param['code'];
  194. $icard['card_id'] = $param['card_id'];
  195. $icard['record_bonus'] = $param['record_bonus'];
  196. $icard['bonus'] = $param['bonus'];
  197. $icard['add_bonus'] = $param['add_bonus'];
  198. $api = $this->wechat_mp->card;
  199. $res = $api->httpPostJson('/card/membercard/updateuser', $icard);
  200. /**
  201. * {
  202. "errcode": 0,
  203. "errmsg": "ok",
  204. "result_bonus": 3000,
  205. "result_balance": 0,
  206. "openid": "oBolB1pon-jHiNVRZ8fgD8NMfxHg"
  207. }
  208. */
  209. \Yii::error([__METHOD__, $param, $icard, $res]);
  210. return $res;
  211. }
  212. private function card_membercard_activate_geturl($card_id)
  213. {
  214. $json = '{
  215. "card_id": "pBolB1m7MANGetesAPo8LpdJVeYk",
  216. }';
  217. $icard = json_decode($json, true);
  218. $icard['card_id'] = $card_id;
  219. $api = $this->wechat_mp->card;
  220. $res = $api->httpPostJson('/card/membercard/activate/geturl', $icard);
  221. /**
  222. * {
  223. "errcode": 0,
  224. "errmsg": "ok",
  225. "url": "https://mp.weixin.qq.com/bizmall/activatemembercard?action=preshow&&encrypt_card_id=QjjuuGjJMbUHVOF8Me5raozNfHEGeVP6Um31MiJDw7pxLas%2Bvp%2FXRXS%2FT%2BRNUVMh&biz=MzU5NDYxOTE0Mg%3D%3D#wechat_redirect"
  226. }
  227. */
  228. \Yii::error([__METHOD__, $res]);
  229. return $res;
  230. }
  231. private function card_membercard_activateuserform_set($card_id)
  232. {
  233. $json = '{
  234. "card_id": "pBolB1m7MANGetesAPo8LpdJVeYk",
  235. "required_form": {
  236. "can_modify": false,
  237. "common_field_id_list": [
  238. "USER_FORM_INFO_FLAG_MOBILE"
  239. ]
  240. }
  241. }';
  242. $icard = json_decode($json, true);
  243. $icard['card_id'] = $card_id;
  244. $api = $this->wechat_mp->card;
  245. $res = $api->httpPostJson('/card/membercard/activateuserform/set', $icard);
  246. /**
  247. * {
  248. "errcode": 0,
  249. "errmsg": "ok"
  250. }
  251. */
  252. \Yii::error([__METHOD__, $res]);
  253. return $res;
  254. }
  255. private function card_create(&$wx_materials, $param, $store_id) {
  256. $form = new WechatConfigForm();
  257. $form->store_id = $store_id;
  258. $form->type = 1;
  259. $conf = $form->getConfig();
  260. $mini_user_name = $conf['data']['mini']['gh_wechat_app_id'];
  261. $mini_page = '/pages/my/my';
  262. $json = '{
  263. "card": {
  264. "card_type": "MEMBER_CARD",
  265. "member_card": {
  266. "wx_activate":true,
  267. "auto_activate": false,
  268. "wx_activate_after_submit" : false,
  269. "background_pic_url": "https://mmbiz.qlogo.cn/mmbiz/",
  270. "base_info": {
  271. "logo_url": "http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZ/0",
  272. "brand_name": "海底捞",
  273. "code_type": "CODE_TYPE_TEXT",
  274. "title": "海底捞会员卡5",
  275. "color": "Color010",
  276. "notice": "使用时向服务员出示此券",
  277. "service_phone": "020-88888888",
  278. "description": "\n不可与其他优惠同享\n",
  279. "date_info": {
  280. "type": "DATE_TYPE_PERMANENT"
  281. },
  282. "sku": {
  283. "quantity": 50000000
  284. },
  285. "get_limit": 1,
  286. "use_custom_code": false,
  287. "can_give_friend": false,
  288. "use_all_locations": true,
  289. "custom_url_name": "会员中心",
  290. "custom_url": "http://weixin.qq.com",
  291. "custom_app_brand_user_name": "' . $mini_user_name . '@app",
  292. "custom_app_brand_pass":"' . $mini_page . '",
  293. "custom_url_sub_title": "查询更多信息"
  294. },
  295. "advanced_info": {
  296. "abstract": {
  297. "abstract": "微信餐厅推出多种新季菜品,期待您的光临"
  298. }
  299. },
  300. "supply_bonus": true,
  301. "supply_balance": false,
  302. "prerogative": "特权说明"
  303. }
  304. }
  305. }';
  306. $icard = json_decode($json, true);
  307. $background_pic_url = $this->wxUploadImage($param['background_pic_url']);
  308. if (isset($background_pic_url['code']) && $background_pic_url['code'] === 1) {
  309. return $background_pic_url;
  310. }
  311. $logo_url = $this->wxUploadImage($param['logo_url']);
  312. if (isset($logo_url['code']) && $logo_url['code'] === 1) {
  313. return $logo_url;
  314. }
  315. $wx_materials[] = $background_pic_url['media_id'];
  316. $wx_materials[] = $logo_url['media_id'];
  317. $icard['card']['member_card']['background_pic_url'] = $background_pic_url['url'];
  318. $icard['card']['member_card']['base_info']['logo_url'] = $logo_url['url'];
  319. $icard['card']['member_card']['base_info']['brand_name'] = $param['brand_name'];
  320. $icard['card']['member_card']['base_info']['title'] = $param['title'];
  321. $icard['card']['member_card']['base_info']['service_phone'] = $param['service_phone'];
  322. $icard['card']['member_card']['base_info']['description'] = $param['description'];
  323. $icard['card']['member_card']['advanced_info']['abstract']['abstract'] = $param['abstract'];
  324. $param['text_image_list'] && $icard['card']['member_card']['advanced_info']['text_image_list'] = $param['text_image_list'];
  325. $icard['card']['member_card']['prerogative'] = $param['prerogative'];
  326. $api = $this->wechat_mp->card;
  327. $res = $api->httpPostJson('/card/create', $icard);
  328. /**
  329. * [
  330. 'errcode' => 0,
  331. 'errmsg' => 'ok',
  332. 'card_id' => 'pBolB1i00fKL6dqh8ojvMqPAJXfg',
  333. ]
  334. */
  335. \Yii::error([__METHOD__, $res]);
  336. return $res;
  337. }
  338. private function card_update(&$wx_materials, $param) {
  339. $json = '{
  340. "card_id": "ph_gmt7cUVrlRk8swPwx7aDyF-pg",
  341. "member_card": {
  342. "background_pic_url": "https://mmbiz.qlogo.cn/mmbiz/0?wx_fmt=jpeg",
  343. "base_info": {
  344. "title": "海底捞会员卡5",
  345. "logo_url": "http:\/\/www.supadmin.cn\/uploads\/allimg\/120216\/1_120216214725_1.jpg",
  346. "color": "Color010",
  347. "notice": "使用时向服务员出示此券",
  348. "service_phone": "020-88888888",
  349. "description": "\n不可与其他优惠同享\n"
  350. },
  351. "prerogative": ""
  352. }
  353. }';
  354. $icard = json_decode($json, true);
  355. $background_pic_url = $this->wxUploadImage($param['background_pic_url']);
  356. if (isset($background_pic_url['code']) && $background_pic_url['code'] === 1) {
  357. return $background_pic_url;
  358. }
  359. $logo_url = $this->wxUploadImage($param['logo_url']);
  360. if (isset($logo_url['code']) && $logo_url['code'] === 1) {
  361. return $logo_url;
  362. }
  363. $wx_materials[] = $background_pic_url['media_id'];
  364. $wx_materials[] = $logo_url['media_id'];
  365. $icard['card_id'] = $param['card_id'];
  366. $icard['member_card']['background_pic_url'] = $background_pic_url['url'];
  367. $icard['member_card']['base_info']['logo_url'] = $logo_url['url'];
  368. $icard['member_card']['base_info']['title'] = $param['title'];
  369. $icard['member_card']['base_info']['service_phone'] = $param['service_phone'];
  370. $icard['member_card']['base_info']['description'] = $param['description'];
  371. $icard['member_card']['prerogative'] = $param['prerogative'];
  372. $api = $this->wechat_mp->card;
  373. $res = $api->httpPostJson('/card/update', $icard);
  374. /**
  375. * {
  376. "errcode":0,
  377. "errmsg":"ok",
  378. "send_check":true
  379. }
  380. */
  381. \Yii::error([__METHOD__, $res]);
  382. return $res;
  383. }
  384. //获取卡参数
  385. public function getUrl($card_id) {
  386. $res = [
  387. 'code' => 0,
  388. 'msg' => 'ok',
  389. 'encrypt_card_id' => '',
  390. 'biz' => '',
  391. 'url' => '',
  392. ];
  393. $wxres = $this->card_membercard_activate_geturl($card_id);
  394. if($wxres['errcode'] == 0 && !empty($wxres['url'])){
  395. $arr = parse_url($wxres['url']);
  396. parse_str($arr['query'], $query);
  397. $res['encrypt_card_id'] = $query['encrypt_card_id'];
  398. $res['biz'] = $query['biz'];
  399. $res['url'] = $wxres['url'];
  400. $res['card_id'] = $card_id;
  401. }
  402. return $res;
  403. }
  404. //获取卡参数
  405. public function getUrlByStoreId($store_id) {
  406. $card = $this->getCardInfoByStoreId($store_id);
  407. if(!$card){
  408. return [
  409. 'code' => 1,
  410. 'msg' => '未找到卡信息',
  411. ];
  412. }
  413. $card_id = $card->card_id;
  414. return $this->getUrl($card_id);
  415. }
  416. //获取会员
  417. public function getUser($code, $card_id) {
  418. $res = [
  419. 'code' => 1,
  420. 'msg' => 'faild',
  421. ];
  422. $wxres = $this->card_membercard_userinfo_get($code, $card_id);
  423. $res['data'] = $wxres;
  424. if($wxres['errcode'] == 0){
  425. $res['code'] = 0;
  426. $res['msg'] = 'ok';
  427. }
  428. return $res;
  429. }
  430. //获取会员手机号
  431. public function getPhone($code, $card_id) {
  432. $wxres = $this->card_membercard_userinfo_get($code, $card_id);
  433. if($wxres['errcode'] == 0 && is_array($wxres['user_info']['common_field_list'])){
  434. foreach ($wxres['user_info']['common_field_list'] as $item) {
  435. if($item['name'] == 'USER_FORM_INFO_FLAG_MOBILE'){
  436. return $item['value'];
  437. }
  438. }
  439. }
  440. return '';
  441. }
  442. //绑卡
  443. public function bindSaas($code, $card_id, $openid, $OuterStr) {
  444. $phone = $this->getPhone($code, $card_id);
  445. if($phone){
  446. $cardInfo = $this->getCardInfo($card_id);
  447. if(!$cardInfo){
  448. \yii::error([__METHOD__, '系统中没有找到会员卡', $phone, $card_id]);
  449. return [
  450. 'code' => 1,
  451. 'message' => '系统中没有找到会员卡',
  452. ];
  453. }
  454. $user = User::findOne(['store_id' => $cardInfo->store_id, 'binding' => $phone]);
  455. if(!$user){
  456. \yii::error([__METHOD__, '领卡手机号在系统中没有找到会员', $phone, $cardInfo]);
  457. return [
  458. 'code' => 1,
  459. 'message' => '领卡手机号在系统中没有找到会员',
  460. ];
  461. }
  462. $hasBind = $this->getBind($card_id, $user->id);
  463. if($hasBind){
  464. \yii::error([__METHOD__, '当前会员已经绑定过此会员卡', $card_id, $user->id, $hasBind]);
  465. return [
  466. 'code' => 1,
  467. 'message' => '当前会员已经绑定过此会员卡',
  468. ];
  469. }
  470. $save = $this->saveBind([
  471. 'store_id' => $cardInfo->store_id,
  472. 'card_id' => $card_id,
  473. 'code' => $code,
  474. 'phone' => $phone,
  475. 'openid' => $openid,
  476. 'user_id' => $user->id,
  477. 'outer_str' => $OuterStr,
  478. ]);
  479. if($save && $user->integral){
  480. $res = $this->updateUser($this->getBind($card_id, $user->id), [
  481. "record_bonus" => "首次同步系统积分",
  482. "bonus" => $user->integral,
  483. "add_bonus" => 0,
  484. ]);
  485. return $res;
  486. }
  487. }else{
  488. \yii::error([__METHOD__, '没有查到手机号', $code, $card_id]);
  489. return [
  490. 'code' => 1,
  491. 'message' => '没有查到手机号',
  492. ];
  493. }
  494. return [
  495. 'code' => 1,
  496. 'message' => '失败',
  497. ];
  498. }
  499. //获取卡列表
  500. public function getCardList($params = [], $store_id = 0) {
  501. $query = $this->cardModel::find()->where(['is_delete' => 0]);
  502. if(isset($params['card_store_id']) && $params['card_store_id'] > 0){
  503. $query->andWhere(['store_id' => $params['card_store_id']]);
  504. }
  505. if(!empty($params['card_id'])){
  506. $query->andWhere(['card_id' => $params['card_id']]);
  507. }
  508. if(!empty($params['title'])){
  509. $query->andWhere(['title' => $params['title']]);
  510. }
  511. if(!empty($params['brand_name'])){
  512. $query->andWhere(['brand_name' => $params['brand_name']]);
  513. }
  514. if($store_id > 0){
  515. $query->andWhere(['store_id' => $store_id]);
  516. }
  517. $query->orderBy('id DESC');
  518. $data = pagination_make($query);
  519. foreach ($data['list'] as &$item) {
  520. $wxRes = $this->card_get($item['card_id']);
  521. $item['wxRes'] = $wxRes;
  522. }
  523. return [
  524. 'code' => 0,
  525. 'msg' => 'ok',
  526. 'data' => $data,
  527. 'store_list' => Store::nameList(),
  528. ];
  529. }
  530. //获取卡信息
  531. public function getCardInfo($card_id) {
  532. $card = $this->cardModel::findOne(['card_id' => $card_id]);
  533. return $card;
  534. }
  535. //获取卡信息
  536. public function getCardInfoByStoreId($store_id) {
  537. $card = $this->cardModel::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  538. return $card;
  539. }
  540. //保存卡
  541. public function saveCardInfo($wx_materials, $card_id, $param, $store_id = 0) {
  542. $model = $this->cardModel;
  543. $card = $model->findOne(['card_id' => $card_id]);
  544. if($card){
  545. $model = $card;
  546. $materials = json_decode($model->wx_materials, true);
  547. if($materials){
  548. $wx_materials = array_merge($materials, $wx_materials);
  549. }
  550. $model->title = $param['title'];
  551. $model->logo_url = $param['logo_url'];
  552. $model->background_pic_url = $param['background_pic_url'];
  553. $model->prerogative = $param['prerogative'];
  554. $model->wx_materials = json_encode($wx_materials);
  555. $save = $model->save();
  556. \Yii::error($model->getErrors());
  557. return $save;
  558. }
  559. $model->card_id = $card_id;
  560. $store_id > 0 && $model->store_id = $store_id;
  561. $model->title = $param['title'];
  562. $model->brand_name = $param['brand_name'];
  563. $model->logo_url = $param['logo_url'];
  564. $model->background_pic_url = $param['background_pic_url'];
  565. $model->prerogative = $param['prerogative'];
  566. $model->wx_materials = json_encode($wx_materials);
  567. $save = $model->save();
  568. \Yii::error($model->getErrors());
  569. return $save;
  570. }
  571. //获取绑卡列表查询条件
  572. private function getBindListQuery($params = [], $store_id = 0) {
  573. $query = $this->bindModel::find()->where(['b.is_delete' => 0]);
  574. $query->alias('b')->leftJoin(['c' => $this->cardModel->tableName()], 'b.card_id = c.card_id')->andWhere(['c.is_delete' => 0]);
  575. if(isset($params['card_store_id']) && $params['card_store_id'] > 0){
  576. $query->andWhere(['b.store_id' => $params['card_store_id']]);
  577. }
  578. if(!empty($params['card_id'])){
  579. $query->andWhere(['b.card_id' => $params['card_id']]);
  580. }
  581. if(!empty($params['code'])){
  582. $query->andWhere(['b.code' => $params['code']]);
  583. }
  584. if(!empty($params['phone'])){
  585. $query->andWhere(['b.phone' => $params['phone']]);
  586. }
  587. if($store_id > 0){
  588. $query->andWhere(['b.store_id' => $store_id]);
  589. $storeCard = $this->getCardInfoByStoreId($store_id);
  590. $query->andWhere(['b.card_id' => $storeCard['card_id']]);
  591. }
  592. return $query;
  593. }
  594. //获取绑卡列表
  595. public function getBindList($params = [], $store_id = 0) {
  596. $query = $this->getBindListQuery($params, $store_id);
  597. $query->orderBy('id DESC');
  598. $data = pagination_make($query);
  599. foreach ($data['list'] as &$item) {
  600. $user = User::findOne($item['user_id']);
  601. $item['nickname'] = $user->nickname;
  602. $item['avatar_url'] = $user->avatar_url;
  603. }
  604. return [
  605. 'code' => 0,
  606. 'msg' => 'ok',
  607. 'data' => $data,
  608. 'sql' => $query->createCommand()->getRawSql(),
  609. 'store_list' => Store::nameList(),
  610. ];
  611. }
  612. //获取绑卡ID列表
  613. public function getBindIdList($params = [], $store_id = 0) {
  614. $query = $this->getBindListQuery($params, $store_id);
  615. $query->select('b.id');
  616. $list = $query->column();
  617. return $list;
  618. }
  619. //获取绑卡信息
  620. public function getBind($card_id, $user_id) {
  621. $bind = $this->bindModel::findOne([
  622. 'card_id' => $card_id,
  623. 'user_id' => $user_id,
  624. ]);
  625. return $bind;
  626. }
  627. //获取绑卡信息
  628. public function getBindById($id) {
  629. $bind = $this->bindModel::findOne($id);
  630. return $bind;
  631. }
  632. //获取绑卡信息
  633. public function getBindByUserId($user_id, $store_id) {
  634. $card = $this->getCardInfoByStoreId($store_id);
  635. if(!$card){
  636. return false;
  637. }
  638. $card_id = $card->card_id;
  639. return $this->getBind($card_id, $user_id);
  640. }
  641. //绑卡
  642. public function saveBind($param) {
  643. $model = $this->bindModel;
  644. $model->attributes = $param;
  645. $save = $model->save();
  646. \Yii::error($model->getErrors());
  647. return $save;
  648. }
  649. //创建卡
  650. public function addCard($param, $store_id = 0) {
  651. $hasCard = $this->getCardInfoByStoreId($store_id);
  652. if($hasCard){
  653. return [
  654. 'code' => 1,
  655. 'msg' => '当前已存在会员卡,创建新卡之前请删除旧卡:' . $hasCard->title,
  656. ];
  657. }
  658. $wx_materials = [];
  659. $wxres = $this->card_create($wx_materials, $param, $store_id);
  660. if (isset($wxres['code']) && $wxres['code'] === 1) {
  661. return $wxres;
  662. }
  663. if($wxres['errcode'] == 0 && !empty($wxres['card_id'])){
  664. $card_id = $wxres['card_id'];
  665. $wxres = $this->card_membercard_activateuserform_set($card_id);
  666. if($wxres['errcode'] == 0){
  667. $res = $this->saveCardInfo($wx_materials, $card_id, $param, $store_id);
  668. if($res){
  669. return [
  670. 'code' => 0,
  671. 'msg' => '成功',
  672. ];
  673. }
  674. }
  675. }
  676. $this->wxDelMaterial($wx_materials);
  677. if($wxres['errcode'] > 0){
  678. return [
  679. 'code' => $wxres['errcode'],
  680. 'msg' => $wxres['errmsg'],
  681. ];
  682. }
  683. return [
  684. 'code' => 1,
  685. 'msg' => '操作失败',
  686. ];
  687. }
  688. //修改卡
  689. public function updateCard($param, $store_id = 0) {
  690. $wx_materials = [];
  691. $wxres = $this->card_update($wx_materials, $param);
  692. if (isset($wxres['code']) && $wxres['code'] === 1) {
  693. return $wxres;
  694. }
  695. if($wxres['errcode'] == 0){
  696. $card_id = $param['card_id'];
  697. $res = $this->saveCardInfo($wx_materials, $card_id, $param, $store_id);
  698. if($res){
  699. return [
  700. 'code' => 0,
  701. 'msg' => '成功',
  702. ];
  703. }
  704. }
  705. $this->wxDelMaterial($wx_materials);
  706. if($wxres['errcode'] > 0){
  707. return [
  708. 'code' => $wxres['errcode'],
  709. 'msg' => $wxres['errmsg'],
  710. ];
  711. }
  712. return [
  713. 'code' => 1,
  714. 'msg' => '操作失败',
  715. ];
  716. }
  717. //删除卡
  718. public function deleteCard($card_id) {
  719. $card = $this->getCardInfo($card_id);
  720. $card->is_delete = 1;
  721. $res = $card->save();
  722. if($res){
  723. $wx_materials = json_decode($card->wx_materials, true);
  724. if($wx_materials){
  725. $this->wxDelMaterial($wx_materials);
  726. }
  727. return [
  728. 'code' => 0,
  729. 'msg' => '成功',
  730. ];
  731. }
  732. return [
  733. 'code' => 1,
  734. 'msg' => '操作失败',
  735. ];
  736. }
  737. //修改会员积分
  738. public function updateUserBonus($bind, $param) {
  739. $res = AccountLog::saveLog($bind->user_id, $param['add_bonus'], AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, $param['record_bonus']);
  740. if($res){
  741. return [
  742. 'code' => 0,
  743. 'msg' => '成功',
  744. ];
  745. }
  746. return [
  747. 'code' => 1,
  748. 'msg' => '操作失败',
  749. ];
  750. }
  751. //修改会员信息
  752. public function updateUser($bind, $param) {
  753. // $param = [
  754. // "record_bonus" => "首次同步系统积分",
  755. // "bonus" => $user->integral,
  756. // "add_bonus" => 0,
  757. // ];
  758. $param['card_id'] = $bind->card_id;
  759. $param['code'] = $bind->code;
  760. $wxres = $this->card_membercard_updateuser($param);
  761. if($wxres['errcode'] == 0){
  762. return [
  763. 'code' => 0,
  764. 'msg' => '成功',
  765. ];
  766. }
  767. return [
  768. 'code' => $wxres['errcode'],
  769. 'msg' => $wxres['errmsg'],
  770. ];
  771. }
  772. //批量修改会员信息
  773. public function batchUpdateUserBonus($bind_ids, $param) {
  774. $okCount = 0;
  775. $count = 100;
  776. $count = 1;
  777. $chunk = array_chunk($bind_ids, $count);
  778. foreach($chunk as $ids){
  779. $queue = queue_push(new WxBonusJob(['bind_ids' => $ids, 'param' => $param]));
  780. if($queue){
  781. $okCount += count($ids);
  782. }
  783. }
  784. if($okCount > 0){
  785. return [
  786. 'code' => 0,
  787. 'msg' => '操作成功,入队列数量:' . $okCount,
  788. ];
  789. }
  790. return [
  791. 'code' => 1,
  792. 'msg' => '操作失败',
  793. ];
  794. }
  795. }