params; $appid = $params['app_id']; $id = $params['id']; $store_id = $this->store_id; $app_secret = $params['app_secret']; $token = $params['token']; $encodingAesKey = $params['encodingAesKey']; $finderUserName = $params['finderUserName']; if ($appid && $app_secret) { try { $config = []; $config['app_id'] = $appid; $config['secret'] = $app_secret; $config['response_type'] = 'array'; $miniProgram = Factory::miniProgram($config); $client = new BaseClient($miniProgram); $client->httpGet('channels/ec/basics/info/get'); } catch (\Exception $e) { throw new \Exception('App Id或 App Secret(小店密钥)错误'); } } $where = [ 'is_self' => 1, 'is_cancle' => 0 ]; if ($id) { $where = array_merge($where, [ 'id' => $id ]); } elseif ($appid) { $where = array_merge($where, [ 'appid' => $appid, 'store_id' => $store_id, ]); } $store_mini = StoreMini::findOne($where); if (!$store_mini) { $store_mini = new StoreMini(); } $store_mini->appid = $appid; if ($app_secret) { $store_mini->app_secret = $app_secret; } else { if (empty($store_mini->id)) { throw new \Exception('添加需要填写app_secret'); } } $store_mini->is_self = 1; $store_mini->store_id = $store_id; $store_mini->fuwu_type = 1; if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors, JSON_UNESCAPED_UNICODE)); } if ($token) { Option::set('token', $token, $store_id, 'video_shop_config_' . $store_mini->id); } if ($encodingAesKey) { Option::set('encodingAesKey', $encodingAesKey, $store_id, 'video_shop_config_' . $store_mini->id); } if ($finderUserName) { Option::set('finderUserName', $finderUserName, $store_id, 'video_shop_config_' . $store_mini->id); } return [ 'code' => 0, 'msg' => '保存成功', 'data' => $store_mini, ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function delShop() { try { $params = $this->params; $ids = $this->ids; $store_id = $this->store_id; $ids = explode(',', $ids); StoreMini::updateAll(['is_cancle' => 1], ['id' => $ids, 'store_id' => $store_id]); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //邀请 public function inviteSharer() { $t = \Yii::$app->db->beginTransaction(); try { $store_id = $this->store_id; $mini_id = $this->mini_id; $params = $this->params; $result = $this->checkSave(); if ($result['code'] === 1) { return $result; } $client = new BaseClient($this->miniProgram); $data = [ 'username' => $params['username'], ]; $res = $client->httpPostJson('channels/ec/sharer/bind', $data); $file_name = md5(date('YmdHis') .'sharer_bind_'. $params['username'] ) . '.jpg'; if (!$res['errcode'] && !empty($res)) { if (!is_dir(\Yii::$app->runtimePath . '/image')) { mkdir(\Yii::$app->runtimePath . '/image'); } file_put_contents(\Yii::$app->runtimePath . '/image/' . $file_name, base64_decode($res['qrcode_img_base64'])); $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'username' => $params['username'], 'is_delete' => 0, 'action_id' => $mini_id]); // $image = imagecreatefromstring($res['qrcode_img']); //将二进制流转换成图片资源 // imagepng($image, \Yii::$app->runtimePath . '/image/' . $file_name); //保存PNG格式的图片 if (!$sharer) { $sharer = new VideoShopSharer(); $sharer->username = $data['username']; $sharer->store_id = $store_id; $sharer->action_id = $mini_id; $sharer->is_share = 1; } $sharer->is_use = 0; $sharer->is_bind = 1; $sharer->invite_image = \Yii::$app->request->hostInfo . '/runtime/image/' . $file_name; $sharer->share_level = $params['level']; $sharer->user_id = $params['user_id']; if (!$sharer->save()) { throw new \Exception(json_encode($sharer->errors, JSON_UNESCAPED_UNICODE)); } $share = Share::findOne(['store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'status' => 1]); if (!$share) { $share = new Share(); $share->user_id = $params['user_id']; $share->mobile = ''; $share->name = ''; $share->is_delete = 0; $share->store_id = $store_id; $share->status = 1; $share->created_at = time(); $share->level = $params['level']; if (!$share->save()) { throw new \Exception(json_encode($share->errors, JSON_UNESCAPED_UNICODE)); } $user = User::findOne($params['user_id']); $user->time = time(); $user->is_distributor = 1; if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } } if (!$share->level) { $share->level = $params['level']; if (!$share->save()) { throw new \Exception(json_encode($share->errors, JSON_UNESCAPED_UNICODE)); } } } else { throw new \Exception($res['errmsg'], $res['errcode']); } $t->commit(); return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'invite_image' => \Yii::$app->request->hostInfo . '/runtime/image/' . $file_name ] ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() ]; } } //同步通过申请员信息 public function getApplySharerInfo() { try { $store_id = $this->store_id; $mini_id = $this->mini_id; // $mini = StoreMini::findOne($this->mini_id); // if (empty($mini->appid)) { // throw new \Exception("当前没有绑定小程序信息"); // } // if (!$this->openPlatform) { // throw new \Exception("参数配置错误"); // } $client = new BaseClient($this->miniProgram); $sharer_list = VideoShopSharer::find()->where(['AND', ['store_id' => $store_id, 'action_id' => $mini_id], ["OR", ['openid' => ''], ['IS', 'openid', NULL]]])->all(); foreach ($sharer_list as $sharer) { $data = [ 'username' => $sharer['username'], ]; $res = $client->httpPostJson('channels/ec/sharer/search_sharer', $data); if (!$res['errcode'] && !empty($res)) { if ($res['openid']) { $sharer->openid = $res['openid']; $sharer->nickname = $res['nickname']; $sharer->bind_time = $res['bind_time']; $sharer->sharer_type = $res['sharer_type']; $sharer->unionid = $res['unionid']; $sharer->is_use = 1; if (!$sharer->save()) { throw new \Exception(json_encode($sharer->errors,JSON_UNESCAPED_UNICODE)); } } } else { throw new \Exception($res['errmsg'], $res['errcode']); } } return [ 'code' => 0, 'msg' => '同步成功' ]; } catch (\Exception $e) { return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() ]; } } /** * @param int $page 页码 * @param int $page_size 每页数据 * @param int $sharer_type 0普通分享员 1店铺分享员 * @return array * @throws \GuzzleHttp\Exception\GuzzleException * 同步店铺分享员数据 */ public function syncSharerList($page = 1, $page_size = 100, $sharer_type = 0) { try { $store_id = $this->store_id; $mini_id = $this->mini_id; // $mini = StoreMini::findOne($this->mini_id); // if (empty($mini->appid)) { // throw new \Exception("当前没有绑定小程序信息"); // } if (!$this->miniProgram) { throw new \Exception("请检查小店状态"); } $client = new BaseClient($this->miniProgram); $data = [ 'page' => $page, 'page_size' => $page_size, 'sharer_type' => $sharer_type ]; $res = $client->httpPostJson('channels/ec/sharer/get_sharer_list', $data); // \Yii::error($res); if (!$res['errcode'] && !empty($res)) { // if (intval($page) === 1) { // VideoShopSharer::updateAll(['is_delete' => 1], ['AND', ['store_id' => $store_id, 'sharer_type' => $sharer_type, 'action_id' => $mini_id], ['<>', 'openid', '']]); // } foreach ($res['sharer_info_list'] as $item) { $sharer = VideoShopSharer::findOne(['openid' => $item['openid'], 'store_id' => $store_id, 'action_id' => $mini_id]); if (!$sharer) { $sharer = new VideoShopSharer(); $sharer->store_id = $store_id; $sharer->openid = $item['openid']; } $sharer->action_id = $mini_id; $sharer->nickname = $item['nickname']; $sharer->bind_time = $item['bind_time']; $sharer->sharer_type = $item['sharer_type']; $sharer->unionid = $item['unionid']; $sharer->is_use = 1; $sharer->is_delete = 0; $sharer->save(); } if (count($res['sharer_info_list']) === intval($page_size) || (count($res['sharer_info_list']) < intval($page_size) && $sharer_type === 0)) { ++$page; if (count($res['sharer_info_list']) < intval($page_size) && $sharer_type === 0) { $sharer_type = 1; $page = 1; } $this->syncSharerList($page, $page_size, $sharer_type); } return [ 'code' => 0, 'msg' => "成功", 'data' => $res['sharer_info_list'] ]; } return [ 'code' => 0, 'data' => [] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取分享员列表 public function getSharerList() { try { $store_id = $this->store_id; $mini_id = $this->mini_id; $nickname = $this->nickname; $username = $this->username; $start_time = $this->start_time; $end_time = $this->end_time; $status = $this->status; $sharer_type = $this->sharer_type; $query = VideoShopSharer::find()->alias('v')->where(['v.store_id' => $this->store_id, 'v.is_delete' => 0, 's.is_cancle' => 0]) ->leftJoin(['s' => StoreMini::tableName()], 'v.action_id = s.id') ->leftJoin(['u' => User::tableName()], 'u.id = v.user_id'); if ($mini_id) { $query->andWhere(['v.action_id' => $mini_id]); } if ($sharer_type !== null &&$sharer_type !== '' && in_array($sharer_type, [0, 1])) { $query->andWhere(['v.sharer_type' => $sharer_type]); } if ($nickname) { $query->andWhere(['OR', ['LIKE', 'v.nickname', $nickname], ['LIKE', 'u.nickname', $nickname]]); } if ($username) { $query->andWhere(['LIKE', 'v.username', $username]); } if ($start_time) { $start_time = strtotime($start_time); $query->andWhere(['>=', 'v.created_at', $start_time]); } if ($end_time) { $end_time = strtotime($end_time); $query->andWhere(['<=', 'v.created_at', $end_time]); } if ($status !== null && intval($status) !== -1) { $query->andWhere(['v.is_bind' => $status]); } $query->select('v.id, v.action_id, v.nickname, v.username, v.is_bind, v.created_at, v.sharer_type, v.share_level, v.is_use, v.invite_image, v.user_id')->orderBy('v.id desc'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $store_mini = StoreMini::findOne($item['action_id']); $item['store_mini_name'] = $store_mini->mini_nickname; $item['is_use'] = (int)$item['is_use']; $item['is_bind'] = (int)$item['is_bind']; $item['sharer_type'] = (int)$item['sharer_type']; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['share_level_name'] = ShareLevel::findOne(['level' => $item['share_level'], 'is_delete' => 0 , 'store_id' => $store_id])->name ?: '普通分销'; $user = User::findOne($item['user_id']); $item['user_name'] = ''; if ($user) { $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); $item['user_name'] = $saas_user->name; } } // 获取小店列表(id 名称) $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'fuwu_type' => 1, 'is_cancle' => 0]) ->select('id, mini_nickname')->asArray()->all(); // // 获取分销商等级 $share_level = ShareLevel::find()->where(['is_delete' => 0, 'store_id' => $store_id]) ->select('level, name')->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], 'store_mini' => $store_mini, 'share_level' => $share_level ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //解绑 public function unbindSharer() { try { $ids = $this->ids; if ($ids) { $ids = explode(',', $ids); } else { throw new \Exception('参数错误'); } $sharer_openid_array = VideoShopSharer::find()->where(['id' => $ids, 'is_delete' => 0])->select('id, openid, action_id')->asArray() ->all(); foreach ($sharer_openid_array as $item) { $miniProgram = (new WechatMini())::getWechatConfig(get_store_id(), $item['action_id'], 1); $client = new BaseClient($miniProgram); $data = [ 'openid_list' => $item['openid'] ]; $res = $client->httpPostJson('channels/ec/sharer/unbind', $data); if (!$res['errcode'] && !empty($res)) { if ($res['refuse_openid']) { $fail_sharer = VideoShopSharer::find()->where(['openid' => $res['refuse_openid'], 'is_delete' => 0, 'id' => $item['id']]) ->select('nickname')->column(); if ($fail_sharer) { $fail_sharer = implode(',', $fail_sharer); throw new \Exception('解绑失败用户:' . $fail_sharer . '。原因可能为openid错误,解绑时间距离绑定成功时间不足1天等'); } } } else { throw new \Exception($res['errmsg'], $res['errcode']); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //编辑 public function editSharerInfo() { $t = \Yii::$app->db->beginTransaction(); try { $params = $this->params; $store_id = $this->store_id; $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'id' => $params['id'], 'is_delete' => 0]); if (!$sharer) { throw new \Exception('信息不存在'); } $this->mini_id = $sharer->action_id; $this->miniProgram = true; $result = $this->checkSave(); if ($result['code'] === 1) { return $result; } if ($params['level']) { $sharer->share_level = $params['level']; if (!$sharer->save()) { throw new \Exception(json_encode($sharer->errors, JSON_UNESCAPED_UNICODE)); } } if (intval($params['user_id']) !== intval($sharer->user_id)) { //将老用户分销商身份删除 $user_id = $sharer->user_id; $share = Share::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]); if ($share) { $share->is_delete = 1; if (!$share->save()) { throw new \Exception(json_encode($share->errors, JSON_UNESCAPED_UNICODE)); } } $user = User::findOne($user_id); if ($user) { $user->time = time(); $user->is_distributor = 0; if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } } // 将新用户修改为分销商 if ($params['user_id']) { $sharer->user_id = $params['user_id']; } $sharer->is_bind = 1; if (!$sharer->save()) { throw new \Exception(json_encode($sharer->errors, JSON_UNESCAPED_UNICODE)); } $share = Share::findOne(['store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'status' => 1]); if (!$share) { $share = new Share(); $share->user_id = $params['user_id']; $share->mobile = ''; $share->name = ''; $share->is_delete = 0; $share->store_id = $store_id; $share->status = 1; $share->created_at = time(); $share->level = $params['level']; if (!$share->save()) { throw new \Exception(json_encode($share->errors, JSON_UNESCAPED_UNICODE)); } $user = User::findOne($params['user_id']); $user->time = time(); $user->is_distributor = 1; if (!$user->save()) { throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE)); } } if (!$share->level && $params['level']) { $share->level = $params['level']; if (!$share->save()) { throw new \Exception(json_encode($share->errors, JSON_UNESCAPED_UNICODE)); } } } $t->commit(); return [ 'code' => 0, 'msg' => '修改完成' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function checkSave() { try { $params = $this->params; $store_id = $this->store_id; $mini_id = $this->mini_id; if (!$this->miniProgram) { throw new \Exception("参数配置错误,请检查小店状态"); } $user = User::findOne(['id' => $params['user_id'], 'is_delete' => 0, 'store_id' => $store_id]); if (!$user) { throw new \Exception("用户不存在"); } $where = ['store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'action_id' => $mini_id]; if ($params['id']) { $where = ['AND', [ 'store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'action_id' => $mini_id ], [ '<>', 'id', $params['id'] ] ]; } $sharer = VideoShopSharer::findOne($where); if ($sharer) { throw new \Exception("商城用户已经被绑定为分享员"); } if (!$params['id']) { $share_level = ShareLevel::findOne(['level' => $params['level'], 'is_delete' => 0, 'store_id' => $store_id]); if (!$share_level) { throw new \Exception("分销商等级不存在"); } $share = Share::findOne(['store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0]); if ($share && $share->level && intval($share->level) !== intval($params['level'])) { throw new \Exception("分销商等级不匹配"); } } return [ 'code' => 0, 'msg' => '检测完成' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }