Relation.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace app\admin\model\wechat;
  3. use app\admin\model\User;
  4. use liuniu\BaseModel;
  5. class Relation extends BaseModel
  6. {
  7. // 表名
  8. protected $name = 'user_relation';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'sex_text',
  18. 'subscribe_text',
  19. 'subscribe_time_text',
  20. 'user_type_text'
  21. ];
  22. public function getSexList()
  23. {
  24. return ['1' => __('Sex 1'), '2' => __('Sex 2'), '0' => __('Sex 0')];
  25. }
  26. public function getSubscribeList()
  27. {
  28. return ['0' => __('Subscribe 0'), '1' => __('Subscribe 1')];
  29. }
  30. public function getUserTypeList()
  31. {
  32. return ['0' => __('User_type 0'), '1' => __('User_type 1'), '2' => __('User_type 2'), '3' => __('User_type 3')];
  33. }
  34. public function getSexTextAttr($value, $data)
  35. {
  36. $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
  37. $list = $this->getSexList();
  38. return isset($list[$value]) ? $list[$value] : '';
  39. }
  40. public function getSubscribeTextAttr($value, $data)
  41. {
  42. $value = $value ? $value : (isset($data['subscribe']) ? $data['subscribe'] : '');
  43. $list = $this->getSubscribeList();
  44. return isset($list[$value]) ? $list[$value] : '';
  45. }
  46. public function getSubscribeTimeTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
  49. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  50. }
  51. public function getUserTypeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
  54. $list = $this->getUserTypeList();
  55. return isset($list[$value]) ? $list[$value] : '';
  56. }
  57. protected function setSubscribeTimeAttr($value)
  58. {
  59. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  60. }
  61. /**
  62. * uid获取小程序Openid
  63. * @param string $uid
  64. * @return bool|mixed
  65. */
  66. public static function getOpenId($uid = '')
  67. {
  68. if ($uid == '') return false;
  69. return self::where('uid', $uid)->value('routine_openid');
  70. }
  71. /**
  72. * TODO 用uid获得openid
  73. * @param $uid
  74. * @param string $openidType
  75. * @return mixed
  76. * @throws \Exception
  77. */
  78. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  79. {
  80. $openid = self::where('uid', $uid)->value($openidType);
  81. return $openid;
  82. }
  83. /**
  84. * TODO 用openid获得uid
  85. * @param $openid
  86. * @param string $openidType
  87. * @return mixed
  88. */
  89. public static function openidTouid($openid, $openidType = 'openid')
  90. {
  91. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  92. }
  93. /**
  94. * 小程序创建用户后返回uid
  95. * @param $routineInfo
  96. * @return mixed
  97. */
  98. public static function routineOauth($routine)
  99. {
  100. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  101. $routineInfo['sex'] = $routine['gender'];//性别
  102. $routineInfo['language'] = $routine['language'];//语言
  103. $routineInfo['city'] = $routine['city'];//城市
  104. $routineInfo['province'] = $routine['province'];//省份
  105. $routineInfo['country'] = $routine['country'];//国家
  106. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  107. $routineInfo['routine_openid'] = $routine['openId'];//openid
  108. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  109. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  110. $routineInfo['user_type'] = 'routine';//用户类型
  111. $spid = 0;//绑定关系uid
  112. $isCOde = false;
  113. //获取是否有扫码进小程序
  114. if ($routine['code']) {
  115. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  116. $spid = $info['third_id'];
  117. $isCOde = true;
  118. } else
  119. $spid = $routine['spid'];
  120. } else if ($routine['spid'])
  121. $spid = $routine['spid'];
  122. // 判断unionid 存在根据unionid判断
  123. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  124. self::edit($routineInfo, $uid, 'uid');
  125. $routineInfo['code'] = $spid;
  126. $routineInfo['isPromoter'] = $isCOde;
  127. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  128. User::updateWechatUser($routineInfo, $uid);
  129. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  130. self::edit($routineInfo, $uid, 'uid');
  131. $routineInfo['code'] = $spid;
  132. $routineInfo['isPromoter'] = $isCOde;
  133. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  134. User::updateWechatUser($routineInfo, $uid);
  135. } else {
  136. $routineInfo['add_time'] = time();//用户添加时间
  137. $routineInfo = self::create($routineInfo);
  138. $res = User::setRoutineUser($routineInfo, $spid);
  139. $uid = $res->uid;
  140. }
  141. return $uid;
  142. }
  143. /**
  144. * 判断是否是小程序用户
  145. * @param int $uid
  146. * @return bool|int|string
  147. */
  148. public static function isRoutineUser($uid = 0)
  149. {
  150. if (!$uid) return false;
  151. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  152. }
  153. /**
  154. * @param int $uid
  155. * @return int
  156. */
  157. public static function isUserStatus($uid = 0)
  158. {
  159. if (!$uid) return 0;
  160. $user = User::getUserInfo($uid);
  161. return $user['status'];
  162. }
  163. /**
  164. * .添加新用户
  165. * @param $openid
  166. * @return object
  167. */
  168. public static function setNewUser($openid)
  169. {
  170. $userInfo = WechatService::getUserInfo($openid);
  171. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  172. exception('请关注公众号!');
  173. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  174. //判断 unionid 是否存在
  175. if (isset($userInfo['unionid'])) {
  176. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  177. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  178. if ($wechatInfo) {
  179. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  180. }
  181. }
  182. self::beginTrans();
  183. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  184. if (!$wechatUser) {
  185. self::rollbackTrans();
  186. exception('用户储存失败!');
  187. }
  188. if (!User::setWechatUser($wechatUser)) {
  189. self::rollbackTrans();
  190. exception('用户信息储存失败!');
  191. }
  192. self::commitTrans();
  193. self::userFirstSubGiveCoupon($openid);
  194. return $wechatUser;
  195. }
  196. /**
  197. * 更新用户信息
  198. * @param $openid
  199. * @return bool
  200. */
  201. public static function updateUser($openid)
  202. {
  203. $userInfo = WechatService::getUserInfo($openid);
  204. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  205. return self::edit($userInfo->toArray(), $openid, 'openid');
  206. }
  207. /**
  208. * 用户存在就更新 不存在就添加
  209. * @param $openid
  210. */
  211. public static function saveUser($openid)
  212. {
  213. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  214. }
  215. /**
  216. * 用户取消关注
  217. * @param $openid
  218. * @return bool
  219. */
  220. public static function unSubscribe($openid)
  221. {
  222. return self::edit(['subscribe' => 0], $openid, 'openid');
  223. }
  224. /**
  225. * TODO 用uid获得Unionid
  226. * @param $uid
  227. * @return mixed
  228. */
  229. public static function uidToUnionid($uid)
  230. {
  231. return self::where('uid', $uid)->value('unionid');
  232. }
  233. /**
  234. * TODO 获取用户信息
  235. * @param $openid
  236. * @param $openidType
  237. * @return array
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. * @throws \think\exception\DbException
  241. */
  242. public static function getWechatInfo($openid, $openidType)
  243. {
  244. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  245. $wechatInfo = self::where($openidType, $openid)->find();
  246. if (!$wechatInfo) {
  247. self::setNewUser($openid);
  248. $wechatInfo = self::where($openidType, $openid)->find();
  249. }
  250. if (!$wechatInfo) exception('获取用户信息失败!');
  251. return $wechatInfo->toArray();
  252. }
  253. }