| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace app\modules\client\models\v1;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\GoodsChainLevel;
- use app\models\GoodsPic;
- use app\models\ShareLevel;
- use app\models\StoreMini;
- use app\models\VideoShopSharer;
- use app\models\VideoShopGoodsExt;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- class VideoShopForm extends Model
- {
- public $params;
- public $store_id;
- public $mini_id;
- public $user;
- public $miniProgram;
- public function rules()
- {
- return [
- [['params', 'store_id', 'mini_id', 'user', 'miniProgram'], 'safe'],
- ];
- }
- //绑定
- public function userSharerList() {
- $store_id = $this->store_id;
- $sharers = VideoShopSharer::find()->alias('vss')
- ->leftJoin(['m' => StoreMini::tableName()], 'm.id = vss.action_id')
- ->where(['vss.store_id' => $store_id, 'vss.user_id' => $this->user->id, 'vss.is_delete' => 0, 'm.is_cancle' => 0])
- ->select('vss.id, vss.nickname, vss.username, vss.is_use, m.mini_nickname')
- ->asArray()->all();
- //分享员小店列表
- $list = [];
- //待办列表
- $list_wait = [];
- foreach($sharers as $item){
- if($item['is_use']){
- $list[] = $item;
- }else{
- $list_wait[] = $item;
- }
- }
- $data = [
- 'list' => $list,
- 'list_wait' => $list_wait,
- ];
- return $data;
- }
- //绑定
- public function bindSharer() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- $params = $this->params;
- $sharer_id = $params['sharer_id'];
-
- if ($this->user->is_distributor != 1) {
- throw new \Exception('操作失败,当前不是分销员');
- }
-
- $wx_username = $params['wx_username'];
- $data = [
- 'username' => $wx_username,
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/sharer/search_sharer', $data);
- if (!$res['errcode'] && !empty($res) && !empty($res['openid'])) {
- $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'openid' => $res['openid'], 'is_delete' => 0, 'action_id' => $mini_id]);
- if($sharer){
- if($sharer->user_id){
- throw new \Exception('操作失败,微信号已录入分享员,且已绑定用户');
- }
- $sharer->user_id = $params['user_id'];
- if (!$sharer->save()) {
- throw new \Exception(array_shift($sharer->getFirstErrors()));
- }
- }else{
- throw new \Exception('操作失败,微信号已是分享员,暂未同步到系统,请等待系统同步后再操作');
- }
- }
- $res = $client->httpPostJson('channels/ec/sharer/bind', $data);
- $file_name = md5(date('YmdHis') .'sharer_bind' ) . '.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']));
- if($sharer_id){
- $sharer = VideoShopSharer::findOne(['id' => $sharer_id, 'store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'action_id' => $mini_id]);
- $sharer->username = $data['username'];
- if (!$sharer->save()) {
- throw new \Exception(array_shift($sharer->getFirstErrors()));
- }
- }else{
- $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'username' => $data['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'] ?? 0;
- $sharer->user_id = $params['user_id'];
- if (!$sharer->save()) {
- throw new \Exception(array_shift($sharer->getFirstErrors()));
- }
- } 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) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|