VideoShopForm.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\modules\client\models\v1;
  3. use app\models\Attr;
  4. use app\models\AttrGroup;
  5. use app\models\Goods;
  6. use app\models\GoodsChainLevel;
  7. use app\models\GoodsPic;
  8. use app\models\ShareLevel;
  9. use app\models\StoreMini;
  10. use app\models\VideoShopSharer;
  11. use app\models\VideoShopGoodsExt;
  12. use EasyWeChat\Kernel\BaseClient;
  13. use yii\base\Model;
  14. class VideoShopForm extends Model
  15. {
  16. public $params;
  17. public $store_id;
  18. public $mini_id;
  19. public $user;
  20. public $miniProgram;
  21. public function rules()
  22. {
  23. return [
  24. [['params', 'store_id', 'mini_id', 'user', 'miniProgram'], 'safe'],
  25. ];
  26. }
  27. //绑定
  28. public function userSharerList() {
  29. $store_id = $this->store_id;
  30. $sharers = VideoShopSharer::find()->alias('vss')
  31. ->leftJoin(['m' => StoreMini::tableName()], 'm.id = vss.action_id')
  32. ->where(['vss.store_id' => $store_id, 'vss.user_id' => $this->user->id, 'vss.is_delete' => 0, 'm.is_cancle' => 0])
  33. ->select('vss.id, vss.nickname, vss.username, vss.is_use, m.mini_nickname')
  34. ->asArray()->all();
  35. //分享员小店列表
  36. $list = [];
  37. //待办列表
  38. $list_wait = [];
  39. foreach($sharers as $item){
  40. if($item['is_use']){
  41. $list[] = $item;
  42. }else{
  43. $list_wait[] = $item;
  44. }
  45. }
  46. $data = [
  47. 'list' => $list,
  48. 'list_wait' => $list_wait,
  49. ];
  50. return $data;
  51. }
  52. //绑定
  53. public function bindSharer() {
  54. $t = \Yii::$app->db->beginTransaction();
  55. try {
  56. $store_id = $this->store_id;
  57. $mini_id = $this->mini_id;
  58. $params = $this->params;
  59. $sharer_id = $params['sharer_id'];
  60. if ($this->user->is_distributor != 1) {
  61. throw new \Exception('操作失败,当前不是分销员');
  62. }
  63. $wx_username = $params['wx_username'];
  64. $data = [
  65. 'username' => $wx_username,
  66. ];
  67. $client = new BaseClient($this->miniProgram);
  68. $res = $client->httpPostJson('channels/ec/sharer/search_sharer', $data);
  69. if (!$res['errcode'] && !empty($res) && !empty($res['openid'])) {
  70. $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'openid' => $res['openid'], 'is_delete' => 0, 'action_id' => $mini_id]);
  71. if($sharer){
  72. if($sharer->user_id){
  73. throw new \Exception('操作失败,微信号已录入分享员,且已绑定用户');
  74. }
  75. $sharer->user_id = $params['user_id'];
  76. if (!$sharer->save()) {
  77. throw new \Exception(array_shift($sharer->getFirstErrors()));
  78. }
  79. }else{
  80. throw new \Exception('操作失败,微信号已是分享员,暂未同步到系统,请等待系统同步后再操作');
  81. }
  82. }
  83. $res = $client->httpPostJson('channels/ec/sharer/bind', $data);
  84. $file_name = md5(date('YmdHis') .'sharer_bind' ) . '.jpg';
  85. if (!$res['errcode'] && !empty($res)) {
  86. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  87. mkdir(\Yii::$app->runtimePath . '/image');
  88. }
  89. file_put_contents(\Yii::$app->runtimePath . '/image/' . $file_name, base64_decode($res['qrcode_img_base64']));
  90. if($sharer_id){
  91. $sharer = VideoShopSharer::findOne(['id' => $sharer_id, 'store_id' => $store_id, 'user_id' => $params['user_id'], 'is_delete' => 0, 'action_id' => $mini_id]);
  92. $sharer->username = $data['username'];
  93. if (!$sharer->save()) {
  94. throw new \Exception(array_shift($sharer->getFirstErrors()));
  95. }
  96. }else{
  97. $sharer = VideoShopSharer::findOne(['store_id' => $store_id, 'username' => $data['username'], 'is_delete' => 0, 'action_id' => $mini_id]);
  98. }
  99. // $image = imagecreatefromstring($res['qrcode_img']); //将二进制流转换成图片资源
  100. // imagepng($image, \Yii::$app->runtimePath . '/image/' . $file_name); //保存PNG格式的图片
  101. if (!$sharer) {
  102. $sharer = new VideoShopSharer();
  103. $sharer->username = $data['username'];
  104. $sharer->store_id = $store_id;
  105. $sharer->action_id = $mini_id;
  106. $sharer->is_share = 1;
  107. }
  108. $sharer->is_use = 0;
  109. $sharer->is_bind = 1;
  110. $sharer->invite_image = \Yii::$app->request->hostInfo . '/runtime/image/' . $file_name;
  111. $sharer->share_level = $params['level'] ?? 0;
  112. $sharer->user_id = $params['user_id'];
  113. if (!$sharer->save()) {
  114. throw new \Exception(array_shift($sharer->getFirstErrors()));
  115. }
  116. } else {
  117. throw new \Exception($res['errmsg'], $res['errcode']);
  118. }
  119. $t->commit();
  120. return [
  121. 'code' => 0,
  122. 'msg' => '获取成功',
  123. 'data' => [
  124. 'invite_image' => \Yii::$app->request->hostInfo . '/runtime/image/' . $file_name
  125. ]
  126. ];
  127. } catch (\Exception $e) {
  128. \Yii::error($e);
  129. $t->rollBack();
  130. return [
  131. 'code' => $e->getCode() ?: 1,
  132. 'msg' => $e->getMessage()
  133. ];
  134. }
  135. }
  136. }