User.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\admin\model\weixin;
  3. use think\Model;
  4. use fast\Random;
  5. use think\Db;
  6. use think\Exception;
  7. use addons\weixin\library\WechatService;
  8. class User extends Model
  9. {
  10. // 表名
  11. protected $name = 'weixin_user';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = false;
  17. protected $deleteTime = false;
  18. // 追加属性
  19. protected $append = [
  20. 'subscribe_time_text'
  21. ];
  22. public function getSubscribeTimeTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
  25. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  26. }
  27. protected function setSubscribeTimeAttr($value)
  28. {
  29. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  30. }
  31. public function fauser()
  32. {
  33. return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
  34. }
  35. /**
  36. * 微信授权成功后
  37. * @param $event
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public static function onWechatOauthAfter($wechatInfo, $spreadId, $login_type)
  43. {
  44. if (isset($wechatInfo['unionid'])) {
  45. $where['unionid'] = $wechatInfo['unionid'];
  46. } else {
  47. $where['openid'] = $wechatInfo['openid'];
  48. }
  49. $uid = self::where($where)->value('uid');
  50. $userextendModel = new \addons\unishop\model\UserExtend();
  51. $d = [];
  52. $d["user_id"] = $uid;
  53. $d["openid"] = $wechatInfo['openid'];
  54. $d["createtime"] = time();
  55. if ($uid) {
  56. //更新微信会员
  57. self::updateWechatUser($wechatInfo, $uid, 'uid');
  58. //检查是否存在主会员数据
  59. if (\app\admin\model\User::get($uid)) {
  60. self::updateUser($wechatInfo, $uid);
  61. }
  62. $extendinfo = $userextendModel->where(["user_id"=>$uid])->find();
  63. if($extendinfo) {
  64. $userextendModel->allowField(true)->save(["openid"=>$wechatInfo['openid'],"updatetime"=>time()], ["user_id" => $uid]);
  65. } else {
  66. $userextendModel->insert($d);
  67. }
  68. } else {
  69. if (isset($wechatInfo['subscribe_scene'])) {
  70. unset($wechatInfo['subscribe_scene']);
  71. }
  72. if (isset($wechatInfo['qr_scene'])) {
  73. unset($wechatInfo['qr_scene']);
  74. }
  75. if (isset($wechatInfo['qr_scene_str'])) {
  76. unset($wechatInfo['qr_scene_str']);
  77. }
  78. $userModel = self::addUser($wechatInfo);
  79. //设置推荐人
  80. $wechatInfo['parent_id'] = $spreadId;
  81. //设置会员主表ID
  82. $wechatInfo['uid'] = $uid = $userModel->id;
  83. //新增微信会员
  84. self::addWechatUser($wechatInfo);
  85. //新增unishop会员-- 支付用
  86. $d = [];
  87. $d["user_id"] = $uid;
  88. $d["openid"] = $wechatInfo['openid'];
  89. $d["createtime"] = time();
  90. $userextendModel->insert($d);
  91. }
  92. return $uid;
  93. }
  94. /**
  95. * uid获取小程序Openid
  96. * @param string $uid
  97. * @return bool|mixed
  98. */
  99. public static function getOpenId($uid = '')
  100. {
  101. if ($uid == '') {
  102. return false;
  103. }
  104. return self::where('uid', $uid)->value('routine_openid');
  105. }
  106. /**
  107. * TODO 用uid获得openid
  108. * @param $uid
  109. * @param string $openidType
  110. * @return mixed
  111. * @throws \Exception
  112. */
  113. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  114. {
  115. $openid = self::where('uid', $uid)->value($openidType);
  116. return $openid;
  117. }
  118. /**
  119. * TODO 用openid获得uid
  120. * @param $openid
  121. * @param string $openidType
  122. * @return mixed
  123. */
  124. public static function openidTouid($openid, $openidType = 'openid')
  125. {
  126. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  127. }
  128. /**
  129. * 添加一条数据
  130. * @param $data
  131. * @param $id
  132. * @param $field
  133. * @return bool $type 返回成功失败
  134. */
  135. public static function addWechatUser($data)
  136. {
  137. $model = new self;
  138. return $model->allowField(true)->save($data);
  139. }
  140. /**
  141. * 修改一条数据
  142. * @param $data
  143. * @param $id
  144. * @param $field
  145. * @return bool $type 返回成功失败
  146. */
  147. public static function updateWechatUser($data, $id, $field = null)
  148. {
  149. $model = new self;
  150. return $model->allowField(true)->save($data, [$field => $id]);
  151. }
  152. /**
  153. * 新建微信会员
  154. * @param $data
  155. * @param $id
  156. * @param $field
  157. * @return bool $type 返回成功失败
  158. */
  159. public static function addUser($wechatUser)
  160. {
  161. $user = false;
  162. //账号注册时需要开启事务,避免出现垃圾数据
  163. Db::startTrans();
  164. try {
  165. $salt = Random::alnum();
  166. $data = [
  167. 'username' => 'wx' . time(),
  168. 'salt' => $salt,
  169. 'password' => md5(md5(time()) . $salt),
  170. 'nickname' => $wechatUser['nickname'] ?: '',
  171. 'avatar' => $wechatUser['headimgurl'] ?: '',
  172. 'jointime' => time(),
  173. 'joinip' => request()->ip(),
  174. 'logintime' => time(),
  175. 'loginip' => request()->ip(),
  176. 'status' => 'normal',
  177. 'group_id' => '1',
  178. ];
  179. $user = \app\admin\model\User::create($data, true);
  180. Db::commit();
  181. } catch (Exception $e) {
  182. Db::rollback();
  183. }
  184. return $user;
  185. }
  186. /**
  187. * 更新用户信息
  188. * @param $wechatUser 用户信息
  189. * @param $uid 用户uid
  190. * @return bool|void
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. * @throws \think\exception\DbException
  194. */
  195. public static function updateUser($wechatUser, $uid)
  196. {
  197. $userInfo = \app\admin\model\User::where('id', $uid)->find();
  198. if (!$userInfo) {
  199. return;
  200. }
  201. $data = [
  202. 'nickname' => $wechatUser['nickname'] ?: '',
  203. 'avatar' => $wechatUser['headimgurl'] ?: '',
  204. 'logintime' => time(),
  205. 'loginip' => request()->ip()
  206. ];
  207. $model = new \app\admin\model\User();
  208. return $model->save($data, ['id' => $uid]);
  209. }
  210. /**
  211. * 得到微信标签
  212. * @author Created by Xing <464401240@qq.com>
  213. */
  214. public static function getTag()
  215. {
  216. $list = Db::name('weixin_cache')->where('key', 'wechat_tag')->value('result');
  217. if (empty($list)) {
  218. try {
  219. $tag = WechatService::userTagService()->lists()->toArray()['tags'] ?: array();
  220. } catch (\Exception $e) {
  221. return array();
  222. }
  223. $list = [];
  224. foreach ($tag as $g) {
  225. $list[$g['id']] = $g;
  226. }
  227. $list = json_encode($list, JSON_UNESCAPED_UNICODE);
  228. Db::name('weixin_cache')->insert([
  229. 'result' => $list, 'add_time' => time(), 'key' => 'wechat_tag'
  230. ]);
  231. }
  232. $list = json_decode($list, true);
  233. return $list;
  234. }
  235. /**
  236. * 得到微信分组
  237. * @author Created by Xing <464401240@qq.com>
  238. */
  239. public static function getGroup()
  240. {
  241. $list = Db::name('weixin_cache')->where('key', 'wechat_group')->value('result');
  242. if (empty($list)) {
  243. try {
  244. $group = WechatService::userGroupService()->lists()->toArray()['groups'] ?: array();
  245. } catch (\Exception $e) {
  246. return array();
  247. }
  248. $list = [];
  249. foreach ($group as $g) {
  250. $list[$g['id']] = $g;
  251. }
  252. $list = json_encode($list, JSON_UNESCAPED_UNICODE);
  253. Db::name('weixin_cache')->insert([
  254. 'result' => $list, 'add_time' => time(), 'key' => 'wechat_group'
  255. ]);
  256. }
  257. $list = json_decode($list, true);
  258. return $list;
  259. }
  260. }