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()); } } }