ShopService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressAddModel;
  4. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressDetailModel;
  5. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressInfoModel;
  6. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressTypeModel;
  7. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\basic\ShopInfoRequest;
  8. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\user\UserAddressAddRequest;
  9. use addons\WechatVideoShop\common\helper\WechatRequestHelper;
  10. use addons\WechatVideoShop\common\models\WechatVideoShop;
  11. use addons\WechatVideoShop\common\models\WechatVideoShopAddress;
  12. use common\enums\StatusEnum;
  13. use common\helpers\FormatHelper;
  14. use yii\helpers\Json;
  15. class ShopService extends AbstractService
  16. {
  17. /**
  18. * 获取店铺id->name键值对
  19. * @param int $mall_id
  20. * @param bool $is_auth
  21. * @return array
  22. */
  23. public function ShopArr(int $mall_id, bool $is_auth = true)
  24. {
  25. $where = [
  26. ['mall_id' => $mall_id],
  27. ['status' => StatusEnum::ENABLED],
  28. ];
  29. if (!empty($is_auth)) $where[] = ['is_auth' => StatusEnum::ENABLED];
  30. $shops = WechatVideoShop::lists([
  31. 'where' => $where,
  32. 'order' => 'id asc',
  33. 'select'=> 'id,name'
  34. ]);
  35. return array_column($shops, 'name', 'id');
  36. }
  37. /**
  38. * 获取店铺信息
  39. * @param int $id
  40. * @return mixed|void
  41. */
  42. public function getShop(int $id)
  43. {
  44. return WechatVideoShop::getOne([
  45. ['id' => $id]
  46. ], true);
  47. }
  48. /**
  49. * 获取第一个店铺ID
  50. * @param int $mall_id
  51. * @return false|int|string|null
  52. */
  53. public function firstShopId(int $mall_id)
  54. {
  55. return WechatVideoShop::find()
  56. ->where(['mall_id' => $mall_id])
  57. ->andWhere(['status' => StatusEnum::ENABLED])
  58. ->select('id')
  59. ->scalar();
  60. }
  61. /**
  62. * @param array $params
  63. * @return true|void
  64. * @throws \Exception
  65. */
  66. public function auth(array $params)
  67. {
  68. if (empty($params)) return true;
  69. /**
  70. * @var $shops WechatVideoShop[]
  71. */
  72. $shops = WechatVideoShop::lists([
  73. 'where' =>
  74. [
  75. ['mall_id' => $params['mall_id']],
  76. ]
  77. ], false);
  78. if (empty($shops)) return true;
  79. try {
  80. foreach ($shops as $shop) {
  81. try {
  82. $resp = WechatRequestHelper::accessTokenAndToArray(new ShopInfoRequest($shop->toArray()), null, false);
  83. } catch (\Exception $e) {
  84. \Yii::error(FormatHelper::exception($e, __METHOD__ . " {$shop->id} 的错误信息"));
  85. continue;
  86. }
  87. if (!empty($resp['info'])) {
  88. $shop->name = $resp['info']['nickname'];
  89. $shop->third_status = $resp['info']['status'];
  90. $shop->logo = $resp['info']['headimg_url'];
  91. $shop->is_auth = StatusEnum::ENABLED;
  92. if (!$shop->save()) throw new \Exception($shop->getErrorMessage());
  93. }
  94. if (isset($resp['errcode']) && $resp['errcode'] != StatusEnum::DISABLED) {
  95. throw new \Exception($resp['errmsg'] ?? Json::encode($resp));
  96. }
  97. // 是否需要创建退货地址
  98. $addressTypeModel = new AddressTypeModel();
  99. $addressTypeModel->pickup = StatusEnum::DISABLED;
  100. $addressTypeModel->same_city = StatusEnum::DISABLED;
  101. $addressInfoModel = new AddressInfoModel();
  102. $addressInfoModel->user_name = $shop->contact_person;
  103. $addressInfoModel->province_name = $shop->province;
  104. $addressInfoModel->city_name = $shop->city;
  105. $addressInfoModel->county_name = $shop->district;
  106. $addressInfoModel->detail_info = $shop->address;
  107. $addressInfoModel->tel_number = $shop->phone;
  108. $addressDetailModel = new AddressDetailModel();
  109. $addressDetailModel->name = $shop->contact_person;
  110. $addressDetailModel->default_send = true;
  111. $addressDetailModel->default_recv = true;
  112. $addressDetailModel->recv_addr = true;
  113. $addressDetailModel->send_addr = true;
  114. $addressDetailModel->address_info = $addressInfoModel;
  115. $addressDetailModel->address_type = $addressTypeModel;
  116. $addressModel = new AddressAddModel();
  117. $addressModel->address_detail = $addressDetailModel;
  118. $hash = md5(Json::encode($addressInfoModel->toArray()));
  119. $addr = WechatVideoShopAddress::getOne([
  120. ['req_hash' => $hash],
  121. ['mall_id' => $params['mall_id']],
  122. ['shop_id' => $shop->id],
  123. ]);
  124. if (empty($addr)) {
  125. $resp = WechatRequestHelper::accessTokenAndToArray(new UserAddressAddRequest($shop->toArray()), $addressModel);
  126. if (empty($resp['address_id'])) throw new \Exception(Json::encode($resp));
  127. $lAddModel = new WechatVideoShopAddress();
  128. $lAddModel->mall_id = $params['mall_id'];
  129. $lAddModel->shop_id = $shop->id;
  130. $lAddModel->province = $shop->province;
  131. $lAddModel->city = $shop->city;
  132. $lAddModel->district = $shop->district;
  133. $lAddModel->address = $shop->address;
  134. $lAddModel->mobile = $shop->phone;
  135. $lAddModel->name = $shop->contact_person;
  136. $lAddModel->req = $addressInfoModel->toArray();
  137. $lAddModel->req_hash = $hash;
  138. $lAddModel->address_id = $resp['address_id'];
  139. if (!$lAddModel->save()) throw new \Exception($lAddModel->getErrorMessage());
  140. }
  141. }
  142. } catch (\Exception $e) {
  143. \Yii::error(FormatHelper::exception($e, __METHOD__));
  144. var_dump($e->getMessage());
  145. }
  146. }
  147. }