User.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if ($uid) {
  51. //更新微信会员
  52. self::updateWechatUser($wechatInfo, $uid, 'uid');
  53. //检查是否存在主会员数据
  54. if (\app\admin\model\User::get($uid)) {
  55. self::updateUser($wechatInfo, $uid);
  56. }
  57. } else {
  58. if (isset($wechatInfo['subscribe_scene'])) {
  59. unset($wechatInfo['subscribe_scene']);
  60. }
  61. if (isset($wechatInfo['qr_scene'])) {
  62. unset($wechatInfo['qr_scene']);
  63. }
  64. if (isset($wechatInfo['qr_scene_str'])) {
  65. unset($wechatInfo['qr_scene_str']);
  66. }
  67. $userModel = self::addUser($wechatInfo);
  68. //设置推荐人
  69. $wechatInfo['parent_id'] = $spreadId;
  70. //设置会员主表ID
  71. $wechatInfo['uid'] = $uid = $userModel->id;
  72. //新增微信会员
  73. self::addWechatUser($wechatInfo);
  74. }
  75. return $uid;
  76. }
  77. /**
  78. * uid获取小程序Openid
  79. * @param string $uid
  80. * @return bool|mixed
  81. */
  82. public static function getOpenId($uid = '')
  83. {
  84. if ($uid == '') {
  85. return false;
  86. }
  87. return self::where('uid', $uid)->value('routine_openid');
  88. }
  89. /**
  90. * TODO 用uid获得openid
  91. * @param $uid
  92. * @param string $openidType
  93. * @return mixed
  94. * @throws \Exception
  95. */
  96. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  97. {
  98. $openid = self::where('uid', $uid)->value($openidType);
  99. return $openid;
  100. }
  101. /**
  102. * TODO 用openid获得uid
  103. * @param $openid
  104. * @param string $openidType
  105. * @return mixed
  106. */
  107. public static function openidTouid($openid, $openidType = 'openid')
  108. {
  109. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  110. }
  111. /**
  112. * 添加一条数据
  113. * @param $data
  114. * @param $id
  115. * @param $field
  116. * @return bool $type 返回成功失败
  117. */
  118. public static function addWechatUser($data)
  119. {
  120. $model = new self;
  121. return $model->allowField(true)->save($data);
  122. }
  123. /**
  124. * 修改一条数据
  125. * @param $data
  126. * @param $id
  127. * @param $field
  128. * @return bool $type 返回成功失败
  129. */
  130. public static function updateWechatUser($data, $id, $field = null)
  131. {
  132. $model = new self;
  133. return $model->allowField(true)->save($data, [$field => $id]);
  134. }
  135. /**
  136. * 新建微信会员
  137. * @param $data
  138. * @param $id
  139. * @param $field
  140. * @return bool $type 返回成功失败
  141. */
  142. public static function addUser($wechatUser)
  143. {
  144. $user = false;
  145. //账号注册时需要开启事务,避免出现垃圾数据
  146. Db::startTrans();
  147. try {
  148. $salt = Random::alnum();
  149. $data = [
  150. 'username' => 'wx' . time(),
  151. 'salt' => $salt,
  152. 'password' => md5(md5(time()) . $salt),
  153. 'nickname' => $wechatUser['nickname'] ?: '',
  154. 'avatar' => $wechatUser['headimgurl'] ?: '',
  155. 'jointime' => time(),
  156. 'joinip' => request()->ip(),
  157. 'logintime' => time(),
  158. 'loginip' => request()->ip(),
  159. 'status' => 'normal',
  160. ];
  161. $user = \app\admin\model\User::create($data, true);
  162. Db::commit();
  163. } catch (Exception $e) {
  164. Db::rollback();
  165. }
  166. return $user;
  167. }
  168. /**
  169. * 更新用户信息
  170. * @param $wechatUser 用户信息
  171. * @param $uid 用户uid
  172. * @return bool|void
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. * @throws \think\exception\DbException
  176. */
  177. public static function updateUser($wechatUser, $uid)
  178. {
  179. $userInfo = \app\admin\model\User::where('id', $uid)->find();
  180. if (!$userInfo) {
  181. return;
  182. }
  183. $data = [
  184. 'nickname' => $wechatUser['nickname'] ?: '',
  185. 'avatar' => $wechatUser['headimgurl'] ?: '',
  186. 'logintime' => time(),
  187. 'loginip' => request()->ip()
  188. ];
  189. $model = new \app\admin\model\User();
  190. return $model->save($data, ['id' => $uid]);
  191. }
  192. /**
  193. * 得到微信标签
  194. * @author Created by Xing <464401240@qq.com>
  195. */
  196. public static function getTag()
  197. {
  198. $list = Db::name('weixin_cache')->where('key', 'wechat_tag')->value('result');
  199. if (empty($list)) {
  200. try {
  201. $tag = WechatService::userTagService()->list()['tags'] ?: array();
  202. } catch (\Exception $e) {
  203. return array();
  204. }
  205. $list = [];
  206. foreach ($tag as $g) {
  207. $list[$g['id']] = $g;
  208. }
  209. $list = json_encode($list, JSON_UNESCAPED_UNICODE);
  210. Db::name('weixin_cache')->insert([
  211. 'result' => $list, 'add_time' => time(), 'key' => 'wechat_tag'
  212. ]);
  213. }
  214. $list = json_decode($list, true);
  215. return $list;
  216. }
  217. }