| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Share;
- use app\models\ShareLevel;
- use app\models\StoreMini;
- use app\models\User;
- use app\models\VideoShopSharer;
- use app\utils\Wechat\WechatMini;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- class VideoShopForm extends Model
- {
- public $params;
- public $store_id;
- public $mini_id;
- public $miniProgram;
- public $ids;
- public $nickname;
- public $username;
- public $start_time;
- public $end_time;
- public $status;
- public $sharer_type;
- public function rules()
- {
- return [
- [['ids', 'nickname', 'username', 'start_time', 'end_time'], 'string'],
- [['status', 'sharer_type', 'mini_id'], 'integer'],
- [['params'], 'safe']
- ];
- }
- public function addShop() {
- try {
- $params = $this->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()
- ];
- }
- }
- }
|