WechatUser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. /**
  16. * TODO 微信用户Model
  17. * Class WechatUser
  18. * @package app\models\user
  19. */
  20. class WechatUser extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'uid';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'wechat_user';
  32. use ModelTrait;
  33. /**
  34. * uid获取小程序Openid
  35. * @param string $uid
  36. * @return bool|mixed
  37. */
  38. public static function getOpenId($uid = '')
  39. {
  40. if ($uid == '') return false;
  41. return self::where('uid', $uid)->value('routine_openid');
  42. }
  43. /**
  44. * TODO 用uid获得openid
  45. * @param $uid
  46. * @param string $openidType
  47. * @return mixed
  48. * @throws \Exception
  49. */
  50. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  51. {
  52. $openid = self::where('uid', $uid)->value($openidType);
  53. return $openid;
  54. }
  55. /**
  56. * TODO 用openid获得uid
  57. * @param $openid
  58. * @param string $openidType
  59. * @return mixed
  60. */
  61. public static function openidTouid($openid, $openidType = 'openid')
  62. {
  63. return self::where($openidType, $openid)->value('uid');
  64. }
  65. /**
  66. * 小程序创建用户后返回uid
  67. * @param $routineInfo
  68. * @return mixed
  69. */
  70. public static function routineOauth($routine)
  71. {
  72. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  73. $routineInfo['sex'] = $routine['gender'];//性别
  74. $routineInfo['language'] = $routine['language'];//语言
  75. $routineInfo['city'] = $routine['city'];//城市
  76. $routineInfo['province'] = $routine['province'];//省份
  77. $routineInfo['country'] = $routine['country'];//国家
  78. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  79. $routineInfo['routine_openid'] = $routine['openId'];//openid
  80. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  81. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  82. $routineInfo['user_type'] = 'routine';//用户类型
  83. $spid = 0;//绑定关系uid
  84. $isCOde = false;
  85. //获取是否有扫码进小程序
  86. if ($routine['code']) {
  87. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  88. $spid = $info['third_id'];
  89. $isCOde = true;
  90. } else
  91. $spid = $routine['spid'];
  92. } else if ($routine['spid'])
  93. $spid = $routine['spid'];
  94. // 判断unionid 存在根据unionid判断
  95. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  96. self::edit($routineInfo, $uid, 'uid');
  97. $routineInfo['code'] = $spid;
  98. $routineInfo['isPromoter'] = $isCOde;
  99. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  100. User::updateWechatUser($routineInfo, $uid);
  101. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  102. self::edit($routineInfo, $uid, 'uid');
  103. $routineInfo['code'] = $spid;
  104. $routineInfo['isPromoter'] = $isCOde;
  105. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  106. User::updateWechatUser($routineInfo, $uid);
  107. } else {
  108. $routineInfo['add_time'] = time();//用户添加时间
  109. $routineInfo = self::create($routineInfo);
  110. $res = User::setRoutineUser($routineInfo, $spid);
  111. $uid = $res->uid;
  112. }
  113. return $uid;
  114. }
  115. /**
  116. * 小程序创建用户后返回uid
  117. * @param $routineInfo
  118. * @return mixed
  119. */
  120. public static function appOauth($routine)
  121. {
  122. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  123. $routineInfo['sex'] = $routine['gender'];//性别
  124. $routineInfo['city'] = $routine['city'];//城市
  125. $routineInfo['province'] = $routine['province'];//省份
  126. $routineInfo['country'] = $routine['country'];//国家
  127. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  128. $routineInfo['routine_openid'] = $routine['openId'];//openid
  129. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  130. $routineInfo['user_type'] = 'h5';//用户类型
  131. $routine['login_type'] = 'h5';//用户类型
  132. $spid = $routine['spid'];
  133. $isCOde = false;
  134. // 判断unionid 存在根据unionid判断
  135. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->value('uid'))) {
  136. self::edit($routineInfo, $uid, 'uid');
  137. $routineInfo['code'] = $spid;
  138. $routineInfo['isPromoter'] = $isCOde;
  139. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  140. User::updateWechatUser($routineInfo, $uid);
  141. } else if ($uid = self::where(['app_openid' => $routineInfo['app_openid']])->value('uid')) { //根据小程序openid判断
  142. self::edit($routineInfo, $uid, 'uid');
  143. $routineInfo['code'] = $spid;
  144. $routineInfo['isPromoter'] = $isCOde;
  145. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  146. User::updateWechatUser($routineInfo, $uid);
  147. } else {
  148. $routineInfo['add_time'] = time();//用户添加时间
  149. $routineInfo = self::create($routineInfo);
  150. $res = User::setRoutineUser($routineInfo, $spid);
  151. $uid = $res->uid;
  152. }
  153. return $uid;
  154. }
  155. /**
  156. * 判断是否是小程序用户
  157. * @param int $uid
  158. * @return bool|int|string
  159. */
  160. public static function isRoutineUser($uid = 0)
  161. {
  162. if (!$uid) return false;
  163. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  164. }
  165. /**
  166. * @param int $uid
  167. * @return int
  168. */
  169. public static function isUserStatus($uid = 0)
  170. {
  171. if (!$uid) return 0;
  172. $user = User::getUserInfo($uid);
  173. return $user['status'];
  174. }
  175. /**
  176. * .添加新用户
  177. * @param $openid
  178. * @return object
  179. */
  180. public static function setNewUser($openid)
  181. {
  182. $userInfo = WechatService::getUserInfo($openid);
  183. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  184. exception('请关注公众号!');
  185. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  186. //判断 unionid 是否存在
  187. if (isset($userInfo['unionid'])) {
  188. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  189. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  190. if ($wechatInfo) {
  191. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  192. }
  193. }
  194. self::beginTrans();
  195. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  196. if (!$wechatUser) {
  197. self::rollbackTrans();
  198. exception('用户储存失败!');
  199. }
  200. if (!User::setWechatUser($wechatUser, 0, $userInfo['site_id'])) {
  201. self::rollbackTrans();
  202. exception('用户信息储存失败!');
  203. }
  204. self::commitTrans();
  205. self::userFirstSubGiveCoupon($openid);
  206. return $wechatUser;
  207. }
  208. /**
  209. * TODO 关注送优惠券
  210. * @param $openid
  211. */
  212. public static function userFirstSubGiveCoupon($openid)
  213. {
  214. $couponIds = StoreCouponIssue::where('status', 1)
  215. ->where('is_give_subscribe', 1)
  216. ->where('is_del', 0)
  217. ->column('cid');
  218. if ($couponIds)
  219. foreach ($couponIds as $couponId)
  220. if ($couponId)
  221. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  222. }
  223. /**
  224. * 订单金额达到预设金额赠送优惠卷
  225. * @param $uid
  226. */
  227. public static function userTakeOrderGiveCoupon($uid, $total_price)
  228. {
  229. $couponIds = StoreCouponIssue::where('status', 1)
  230. ->where('is_full_give', 1)
  231. ->where('is_del', 0)
  232. ->column('cid,full_reduction');
  233. if ($couponIds)
  234. foreach ($couponIds as $couponId)
  235. if ($couponId)
  236. if ($total_price >= $couponId['full_reduction'])
  237. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  238. }
  239. /**
  240. * 更新用户信息
  241. * @param $openid
  242. * @return bool
  243. */
  244. public static function updateUser($openid)
  245. {
  246. $userInfo = WechatService::getUserInfo($openid);
  247. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  248. return self::edit($userInfo->toArray(), $openid, 'openid');
  249. }
  250. /**
  251. * 用户存在就更新 不存在就添加
  252. * @param $openid
  253. */
  254. public static function saveUser($openid)
  255. {
  256. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  257. }
  258. /**
  259. * 用户取消关注
  260. * @param $openid
  261. * @return bool
  262. */
  263. public static function unSubscribe($openid)
  264. {
  265. return self::edit(['subscribe' => 0], $openid, 'openid');
  266. }
  267. /**
  268. * TODO 用uid获得Unionid
  269. * @param $uid
  270. * @return mixed
  271. */
  272. public static function uidToUnionid($uid)
  273. {
  274. return self::where('uid', $uid)->value('unionid');
  275. }
  276. /**
  277. * TODO 获取用户信息
  278. * @param $openid
  279. * @param $openidType
  280. * @return array
  281. * @throws \think\db\exception\DataNotFoundException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. * @throws \think\exception\DbException
  284. */
  285. public static function getWechatInfo($openid, $openidType)
  286. {
  287. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  288. $wechatInfo = self::where($openidType, $openid)->find();
  289. if (!$wechatInfo) {
  290. self::setNewUser($openid);
  291. $wechatInfo = self::where($openidType, $openid)->find();
  292. }
  293. if (!$wechatInfo) exception('获取用户信息失败!');
  294. return $wechatInfo->toArray();
  295. }
  296. }