| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace addons\WechatVideoShop\common\service;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressAddModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressDetailModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressInfoModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\user\AddressTypeModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\basic\ShopInfoRequest;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\user\UserAddressAddRequest;
- use addons\WechatVideoShop\common\helper\WechatRequestHelper;
- use addons\WechatVideoShop\common\models\WechatVideoShop;
- use addons\WechatVideoShop\common\models\WechatVideoShopAddress;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use yii\helpers\Json;
- class ShopService extends AbstractService
- {
- /**
- * 获取店铺id->name键值对
- * @param int $mall_id
- * @param bool $is_auth
- * @return array
- */
- public function ShopArr(int $mall_id, bool $is_auth = true)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ];
- if (!empty($is_auth)) $where[] = ['is_auth' => StatusEnum::ENABLED];
- $shops = WechatVideoShop::lists([
- 'where' => $where,
- 'order' => 'id asc',
- 'select'=> 'id,name'
- ]);
- return array_column($shops, 'name', 'id');
- }
- /**
- * 获取店铺信息
- * @param int $id
- * @return mixed|void
- */
- public function getShop(int $id)
- {
- return WechatVideoShop::getOne([
- ['id' => $id]
- ], true);
- }
- /**
- * 获取第一个店铺ID
- * @param int $mall_id
- * @return false|int|string|null
- */
- public function firstShopId(int $mall_id)
- {
- return WechatVideoShop::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('id')
- ->scalar();
- }
- /**
- * @param array $params
- * @return true|void
- * @throws \Exception
- */
- public function auth(array $params)
- {
- if (empty($params)) return true;
- /**
- * @var $shops WechatVideoShop[]
- */
- $shops = WechatVideoShop::lists([
- 'where' =>
- [
- ['mall_id' => $params['mall_id']],
- ]
- ], false);
- if (empty($shops)) return true;
- try {
- foreach ($shops as $shop) {
- try {
- $resp = WechatRequestHelper::accessTokenAndToArray(new ShopInfoRequest($shop->toArray()), null, false);
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__ . " {$shop->id} 的错误信息"));
- continue;
- }
- if (!empty($resp['info'])) {
- $shop->name = $resp['info']['nickname'];
- $shop->third_status = $resp['info']['status'];
- $shop->logo = $resp['info']['headimg_url'];
- $shop->is_auth = StatusEnum::ENABLED;
- if (!$shop->save()) throw new \Exception($shop->getErrorMessage());
- }
- if (isset($resp['errcode']) && $resp['errcode'] != StatusEnum::DISABLED) {
- throw new \Exception($resp['errmsg'] ?? Json::encode($resp));
- }
- // 是否需要创建退货地址
- $addressTypeModel = new AddressTypeModel();
- $addressTypeModel->pickup = StatusEnum::DISABLED;
- $addressTypeModel->same_city = StatusEnum::DISABLED;
- $addressInfoModel = new AddressInfoModel();
- $addressInfoModel->user_name = $shop->contact_person;
- $addressInfoModel->province_name = $shop->province;
- $addressInfoModel->city_name = $shop->city;
- $addressInfoModel->county_name = $shop->district;
- $addressInfoModel->detail_info = $shop->address;
- $addressInfoModel->tel_number = $shop->phone;
- $addressDetailModel = new AddressDetailModel();
- $addressDetailModel->name = $shop->contact_person;
- $addressDetailModel->default_send = true;
- $addressDetailModel->default_recv = true;
- $addressDetailModel->recv_addr = true;
- $addressDetailModel->send_addr = true;
- $addressDetailModel->address_info = $addressInfoModel;
- $addressDetailModel->address_type = $addressTypeModel;
- $addressModel = new AddressAddModel();
- $addressModel->address_detail = $addressDetailModel;
- $hash = md5(Json::encode($addressInfoModel->toArray()));
- $addr = WechatVideoShopAddress::getOne([
- ['req_hash' => $hash],
- ['mall_id' => $params['mall_id']],
- ['shop_id' => $shop->id],
- ]);
- if (empty($addr)) {
- $resp = WechatRequestHelper::accessTokenAndToArray(new UserAddressAddRequest($shop->toArray()), $addressModel);
- if (empty($resp['address_id'])) throw new \Exception(Json::encode($resp));
- $lAddModel = new WechatVideoShopAddress();
- $lAddModel->mall_id = $params['mall_id'];
- $lAddModel->shop_id = $shop->id;
- $lAddModel->province = $shop->province;
- $lAddModel->city = $shop->city;
- $lAddModel->district = $shop->district;
- $lAddModel->address = $shop->address;
- $lAddModel->mobile = $shop->phone;
- $lAddModel->name = $shop->contact_person;
- $lAddModel->req = $addressInfoModel->toArray();
- $lAddModel->req_hash = $hash;
- $lAddModel->address_id = $resp['address_id'];
- if (!$lAddModel->save()) throw new \Exception($lAddModel->getErrorMessage());
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- var_dump($e->getMessage());
- }
- }
- }
|