WechatUser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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['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. $userInfo['site_id'] = 1;
  184. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  185. exception('请关注公众号!');
  186. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  187. //判断 unionid 是否存在
  188. if (isset($userInfo['unionid'])) {
  189. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  190. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  191. if ($wechatInfo) {
  192. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  193. }
  194. }
  195. self::beginTrans();
  196. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  197. if (!$wechatUser) {
  198. self::rollbackTrans();
  199. exception('用户储存失败!');
  200. }
  201. if (!User::setWechatUser($wechatUser, 0, $userInfo['site_id'])) {
  202. self::rollbackTrans();
  203. exception('用户信息储存失败!');
  204. }
  205. self::commitTrans();
  206. self::userFirstSubGiveCoupon($openid);
  207. return $wechatUser;
  208. }
  209. /**
  210. * TODO 关注送优惠券
  211. * @param $openid
  212. */
  213. public static function userFirstSubGiveCoupon($openid)
  214. {
  215. $couponIds = StoreCouponIssue::where('status', 1)
  216. ->where('is_give_subscribe', 1)
  217. ->where('is_del', 0)
  218. ->column('cid');
  219. if ($couponIds)
  220. foreach ($couponIds as $couponId)
  221. if ($couponId)
  222. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  223. }
  224. /**
  225. * 订单金额达到预设金额赠送优惠卷
  226. * @param $uid
  227. */
  228. public static function userTakeOrderGiveCoupon($uid, $total_price)
  229. {
  230. $couponIds = StoreCouponIssue::where('status', 1)
  231. ->where('is_full_give', 1)
  232. ->where('is_del', 0)
  233. ->column('cid,full_reduction');
  234. if ($couponIds)
  235. foreach ($couponIds as $couponId)
  236. if ($couponId)
  237. if ($total_price >= $couponId['full_reduction'])
  238. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  239. }
  240. /**
  241. * 更新用户信息
  242. * @param $openid
  243. * @return bool
  244. */
  245. public static function updateUser($openid)
  246. {
  247. $userInfo = WechatService::getUserInfo($openid);
  248. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  249. return self::edit($userInfo->toArray(), $openid, 'openid');
  250. }
  251. /**
  252. * 用户存在就更新 不存在就添加
  253. * @param $openid
  254. */
  255. public static function saveUser($openid)
  256. {
  257. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  258. }
  259. /**
  260. * 用户取消关注
  261. * @param $openid
  262. * @return bool
  263. */
  264. public static function unSubscribe($openid)
  265. {
  266. return self::edit(['subscribe' => 0], $openid, 'openid');
  267. }
  268. /**
  269. * TODO 用uid获得Unionid
  270. * @param $uid
  271. * @return mixed
  272. */
  273. public static function uidToUnionid($uid)
  274. {
  275. return self::where('uid', $uid)->value('unionid');
  276. }
  277. /**
  278. * TODO 获取用户信息
  279. * @param $openid
  280. * @param $openidType
  281. * @return array
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. * @throws \think\exception\DbException
  285. */
  286. public static function getWechatInfo($openid, $openidType)
  287. {
  288. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  289. $wechatInfo = self::where($openidType, $openid)->find();
  290. if (!$wechatInfo) {
  291. self::setNewUser($openid);
  292. $wechatInfo = self::where($openidType, $openid)->find();
  293. }
  294. if (!$wechatInfo) exception('获取用户信息失败!');
  295. return $wechatInfo->toArray();
  296. }
  297. }