TouTiaoLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic;
  17. use app\common\logic\BaseLogic;
  18. use app\common\model\User;
  19. use app\common\model\UserAuth;
  20. use app\common\service\ConfigService;
  21. use app\common\service\storage\Driver as StorageDriver;
  22. use app\shopapi\service\UserTokenService;
  23. /**
  24. * 头条逻辑层(字节跳动)
  25. * API文档: https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/server/server-api-introduction
  26. */
  27. class TouTiaoLogic extends BaseLogic
  28. {
  29. private $config;
  30. public function __construct()
  31. {
  32. $this->config = $this->getConfig();
  33. if (empty($this->config['appid']) || empty($this->config['secret'])) {
  34. throw new \Exception("请先在后台配置appid和secret");
  35. }
  36. }
  37. /**
  38. * @notes 静默登录
  39. * @param $params
  40. * @throws \Exception
  41. * @author Tab
  42. * @date 2021/11/10 18:29
  43. */
  44. public function silentLogin($params)
  45. {
  46. // 获取access_token
  47. $access_token = $this->getAccessToken();
  48. // 获取session_key和openId
  49. $result = $this->code2Session($params);
  50. // 获取用户信息
  51. $userInfo = $this->getUserInfo($result);
  52. if(empty($userInfo)){
  53. return [];
  54. }
  55. // 校验账号
  56. $this->checkAccount($userInfo);
  57. // 设置缓存并得到token
  58. $userInfo['token'] = $this->getToken($userInfo, $params);
  59. // 返回用户信息
  60. return $userInfo;
  61. }
  62. /**
  63. * @notes 授权登录
  64. * @param $params
  65. * @throws \Exception
  66. * @author Tab
  67. * @date 2021/11/10 18:29
  68. */
  69. public function authLogin($params)
  70. {
  71. // 获取access_token
  72. $access_token = $this->getAccessToken();
  73. // 获取session_key和openId
  74. $result = $this->code2Session($params);
  75. // 获取用户信息
  76. $userInfo = $this->getUserInfo($result);
  77. if(empty($userInfo)){
  78. $userInfo = $this->createUser($params, $result);
  79. }
  80. // 校验账号
  81. $this->checkAccount($userInfo);
  82. // 设置缓存并得到token
  83. $userInfo['token'] = $this->getToken($userInfo, $params);
  84. // 返回用户信息
  85. return $userInfo;
  86. }
  87. /**
  88. * @notes 获取配置
  89. * @return array
  90. * @author Tab
  91. * @date 2021/11/10 17:08
  92. */
  93. public function getConfig()
  94. {
  95. return [
  96. 'appid' => ConfigService::get("toutiao", "appid", ''),
  97. 'secret' => ConfigService::get("toutiao", "secret", ''),
  98. 'access_token' => ConfigService::get("toutiao", "access_token", ''),
  99. 'expires_in' => ConfigService::get("toutiao", "expires_in", ''),
  100. 'expires_in_time' => ConfigService::get("toutiao", "expires_in_time", ''),
  101. ];
  102. }
  103. /**
  104. * @notes 获取access_token
  105. * @return mixed
  106. * @throws \Exception
  107. * @author Tab
  108. * @date 2021/11/10 18:04
  109. */
  110. public function getAccessToken()
  111. {
  112. // 已获取过access_token并且仍在有效期
  113. if (!empty($this->config['access_token']) && $this->config['expires_in_time'] > time()) {
  114. return $this->config['access_token'];
  115. }
  116. // 重新获取access_token
  117. $url = 'https://developer.toutiao.com/api/apps/v2/token';
  118. $data = [
  119. "appid" => $this->config['appid'],
  120. "secret" => $this->config['secret'],
  121. "grant_type" => 'client_credential'
  122. ];
  123. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  124. $result = $this->http_post($url, $data);
  125. $result = json_decode($result, true);
  126. // 获取成功
  127. if ($result['err_no'] == 0) {
  128. // 获取成功存入数据库
  129. ConfigService::set('toutiao', 'access_token', $result['data']['access_token']);
  130. ConfigService::set('toutiao', 'expires_in', $result['data']['expires_in']);
  131. // 有效期比官方缩短10分钟,提前去重新获取access_token
  132. $expires_in_time = time() + $result['data']['expires_in'] - 600;
  133. ConfigService::set('toutiao', 'expires_in_time', $expires_in_time);
  134. return $result['data']['access_token'];
  135. }
  136. // 获取失败
  137. throw new \Exception("access_token获取失败:" . $result['err_tips']);
  138. }
  139. /**
  140. * @notes 获取session_key和openId
  141. * @param $params
  142. * @throws \Exception
  143. * @author Tab
  144. * @date 2021/11/10 18:29
  145. */
  146. public function code2Session($params)
  147. {
  148. $url = 'https://developer.toutiao.com/api/apps/v2/jscode2session';
  149. $data = [
  150. 'appid' => $this->config['appid'],
  151. 'secret' => $this->config['secret'],
  152. 'code' => $params['code']
  153. ];
  154. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  155. $result = $this->http_post($url, $data);
  156. $result = json_decode($result, true);
  157. // 获取成功
  158. if ($result['err_no'] == 0) {
  159. return $result['data'];
  160. }
  161. // 获取失败
  162. throw new \Exception("openid获取失败:" . $result['err_tips']);
  163. }
  164. /**
  165. * @notes post请求
  166. * @param $url
  167. * @param $data
  168. * @return bool|string
  169. * @throws \Exception
  170. * @author Tab
  171. * @date 2021/11/10 17:36
  172. */
  173. public function http_post($url,$data){
  174. $curl = curl_init();
  175. curl_setopt($curl, CURLOPT_URL, $url);
  176. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  177. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  178. curl_setopt($curl, CURLOPT_POST, true);
  179. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  180. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  181. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Content-Length:' . strlen($data)));
  182. $result = curl_exec($curl);
  183. if(curl_errno($curl)) {
  184. throw new \Exception('Errno'.curl_errno($curl));
  185. }
  186. curl_close($curl);
  187. return $result;
  188. }
  189. /**
  190. * @notes 获取用户信息
  191. * @param $result
  192. * @author Tab
  193. * @date 2021/11/10 18:44
  194. */
  195. public function getUserInfo($result)
  196. {
  197. $user = User::hasWhere('userAuth',['openid'=>$result['openid']],'id,sn,nickname,avatar,mobile,disable')
  198. ->findOrEmpty();
  199. //用户没有该端记录,且返回了unionid,则用unionid找该用户
  200. if($user->isEmpty() && !empty($result['unionid'])){
  201. $user = User::hasWhere('userAuth',['unionid'=>$result['unionid']],'id,sn,nickname,avatar,mobile,disable')
  202. ->findOrEmpty();
  203. }
  204. if ($user->isEmpty()) {
  205. return [];
  206. }
  207. return $user->toArray();
  208. }
  209. /**
  210. * @notes 创建新用户
  211. * @author Tab
  212. * @date 2021/11/10 18:52
  213. */
  214. public function createUser($params, $result)
  215. {
  216. // 获取存储引擎
  217. $config = [
  218. 'default' => ConfigService::get('storage', 'default', 'local'),
  219. 'engine' => ConfigService::get('storage')
  220. ];
  221. //设置头像
  222. if (empty($params['avatarUrl'])) {
  223. //默认头像
  224. $avatar = ConfigService::get('config', 'default_avatar', '');
  225. } else {
  226. if ($config['default'] == 'local') {
  227. $file_name = md5($result['openid'] . time()) . '.jpeg';
  228. $avatar = download_file($params['avatarUrl'], 'uploads/user/avatar/', $file_name);
  229. } else {
  230. $avatar = 'uploads/user/avatar/' . md5($result['openid'] . time()) . '.jpeg';
  231. $StorageDriver = new StorageDriver($config);
  232. if (!$StorageDriver->fetch($params['avatarUrl'], $avatar)) {
  233. throw new Exception( '头像保存失败:'. $StorageDriver->getError());
  234. }
  235. }
  236. }
  237. // 用户编码
  238. $sn = create_user_sn();
  239. // 邀请码
  240. $code = generate_code();
  241. // 生成新用户
  242. $data = [
  243. 'sn' => $sn,
  244. 'nickname' => $params['nickName'],
  245. 'avatar' => $avatar,
  246. 'code' => $code,
  247. 'disable' => 0,
  248. 'register_source' => $params['terminal'],
  249. 'is_register_award' => 0,
  250. ];
  251. $user = User::create($data);
  252. // 用户授权记录
  253. $data = [
  254. 'user_id' => $user->id,
  255. 'openid' => $result['openid'] ?? '',
  256. 'unionid' => $result['unionid'] ?? '',
  257. 'terminal' => $params['terminal'],
  258. ];
  259. UserAuth::create($data);
  260. // 其他业务逻辑
  261. \app\common\logic\UserLogic::registerAward($user->id);
  262. // 返回用户信息
  263. return $user->toArray();
  264. }
  265. /**
  266. * @notes 校验账号
  267. * @param $userInfo
  268. * @throws \Exception
  269. * @author Tab
  270. * @date 2021/11/11 14:17
  271. */
  272. public function checkAccount($userInfo)
  273. {
  274. if ($userInfo['disable']) {
  275. throw new \Exception("账号已禁用,请联系客服处理!");
  276. }
  277. }
  278. /**
  279. * @notes 获取token
  280. * @param $userInfo
  281. * @throws \think\db\exception\DataNotFoundException
  282. * @throws \think\db\exception\DbException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. * @author Tab
  285. * @date 2021/11/11 14:22
  286. */
  287. public function getToken($userInfo, $params)
  288. {
  289. $user = UserTokenService::setToken($userInfo['id'],$params['terminal']);
  290. return $user['token'];
  291. }
  292. }