User.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. use app\common\library\Wechat;
  15. use TencentCloud\Common\Credential;
  16. use TencentCloud\Common\Profile\ClientProfile;
  17. use TencentCloud\Common\Profile\HttpProfile;
  18. use TencentCloud\Common\Exception\TencentCloudSDKException;
  19. use TencentCloud\Faceid\V20180301\FaceidClient;
  20. use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
  21. use TencentCloud\Iai\V20200303\IaiClient;
  22. use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;
  23. /**
  24. * 会员接口,登录,注册,修改资料等
  25. */
  26. class User extends Api
  27. {
  28. protected $noNeedLogin = ['emaillogin','emailregister','mobilelogin','resetpwd'];
  29. protected $noNeedRight = '*';
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. }
  34. /**
  35. * 邮箱登录
  36. *
  37. * @ApiMethod (POST)
  38. * @param string $account 账号
  39. * @param string $password 密码
  40. */
  41. public function emaillogin()
  42. {
  43. $account = input('account');
  44. $password = input('password');
  45. if (!$account || !$password) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. if (!Validate::is($account, 'email')) {
  49. $this->error(__('Email is incorrect'));
  50. }
  51. $ret = $this->auth->login($account, $password);
  52. if ($ret) {
  53. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. //邮箱注册
  59. public function emailregister()
  60. {
  61. $account = input('account');
  62. $captcha = input('captcha');
  63. $password = input('password');
  64. if (!$account || !$captcha || !$password) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::is($account, 'email')) {
  68. $this->error(__('Email is incorrect'));
  69. }
  70. $ret = Ems::check($account, $captcha, 'register');
  71. if (!$ret) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $extend = [
  75. 'register_from' => input('register_from',''),
  76. 'gender' => -1
  77. ];
  78. $ret = $this->auth->register('',$password,$account,'', $extend);
  79. if ($ret) {
  80. Ems::flush($account);
  81. $this->success('注册成功', $this->auth->getUserinfo_simple());
  82. } else {
  83. $this->error($this->auth->getError());
  84. }
  85. }
  86. /**
  87. * 手机验证码登录 + 注册
  88. *
  89. * @ApiMethod (POST)
  90. * @param string $mobile 手机号
  91. * @param string $captcha 验证码
  92. */
  93. public function mobilelogin()
  94. {
  95. $mobile = input('mobile');
  96. $captcha = input('captcha');
  97. if (!$mobile || !$captcha) {
  98. $this->error(__('Invalid parameters'));
  99. }
  100. if (!Validate::regex($mobile, "^1\d{10}$")) {
  101. $this->error(__('Mobile is incorrect'));
  102. }
  103. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  104. $this->error(__('Captcha is incorrect'));
  105. }
  106. $user = \app\common\model\User::getByMobile($mobile);
  107. if ($user) {
  108. if ($user->status == -1) {
  109. $this->error('账户已注销');
  110. }
  111. if (!in_array($user->status,[1,2])) {
  112. $this->error(__('Account is locked'));
  113. }
  114. if ($user->frozentime > time()) {
  115. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  116. }
  117. //如果已经有账号则直接登录
  118. $ret = $this->auth->direct($user->id);
  119. } else {
  120. $extend = [
  121. 'register_from' => input('register_from',''),
  122. 'gender' => -1
  123. ];
  124. $ret = $this->auth->register('', '', '', $mobile, $extend);
  125. }
  126. if ($ret) {
  127. Sms::flush($mobile, 'mobilelogin');
  128. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  129. } else {
  130. $this->error($this->auth->getError());
  131. }
  132. }
  133. /**
  134. * 重置密码
  135. *
  136. * @ApiMethod (POST)
  137. * @param string $mobile 手机号
  138. * @param string $newpassword 新密码
  139. * @param string $captcha 验证码
  140. */
  141. public function resetpwd()
  142. {
  143. //$type = input("type");
  144. $type = 'email';
  145. $mobile = input("mobile");
  146. $email = input("email");
  147. $newpassword = input("newpassword");
  148. $captcha = input("captcha");
  149. if (!$newpassword || !$captcha) {
  150. $this->error(__('Invalid parameters'));
  151. }
  152. if ($type == 'mobile') {
  153. if (!Validate::regex($mobile, "^1\d{10}$")) {
  154. $this->error(__('Mobile is incorrect'));
  155. }
  156. $user = \app\common\model\User::getByMobile($mobile);
  157. if (!$user) {
  158. $this->error(__('User not found'));
  159. }
  160. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  161. if (!$ret) {
  162. $this->error(__('Captcha is incorrect'));
  163. }
  164. Sms::flush($mobile, 'resetpwd');
  165. } else {
  166. if (!Validate::is($email, "email")) {
  167. $this->error(__('Email is incorrect'));
  168. }
  169. $user = \app\common\model\User::getByEmail($email);
  170. if (!$user) {
  171. $this->error(__('User not found'));
  172. }
  173. $ret = Ems::check($email, $captcha, 'resetpwd');
  174. if (!$ret) {
  175. $this->error(__('Captcha is incorrect'));
  176. }
  177. Ems::flush($email, 'resetpwd');
  178. }
  179. //模拟一次登录
  180. $this->auth->direct($user->id);
  181. $ret = $this->auth->changepwd($newpassword, '', true);
  182. if ($ret) {
  183. $this->success(__('Reset password successful'));
  184. } else {
  185. $this->error($this->auth->getError());
  186. }
  187. }
  188. /**
  189. * 手机验证码验证
  190. *
  191. * @ApiMethod (POST)
  192. * @param string $mobile 手机号
  193. * @param string $captcha 验证码
  194. */
  195. /*public function mobilecheck()
  196. {
  197. $mobile = input('mobile');
  198. $captcha = input('captcha');
  199. if (!$mobile || !$captcha) {
  200. $this->error(__('Invalid parameters'));
  201. }
  202. if (!Validate::regex($mobile, "^1\d{10}$")) {
  203. $this->error(__('Mobile is incorrect'));
  204. }
  205. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  206. $this->error(__('Captcha is incorrect'));
  207. }
  208. $this->success('验证通过');
  209. }*/
  210. /**
  211. * 手机验证码注册
  212. *
  213. * @ApiMethod (POST)
  214. * @param string $mobile 手机号
  215. * @param string $captcha 验证码
  216. * @param string $gender 性别:1=男,0=女
  217. */
  218. /*public function mobileregister()
  219. {
  220. $mobile = input('mobile');
  221. $captcha = input('captcha');
  222. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  223. if (!$mobile || !$captcha) {
  224. $this->error(__('Invalid parameters'));
  225. }
  226. if (!Validate::regex($mobile, "^1\d{10}$")) {
  227. $this->error(__('Mobile is incorrect'));
  228. }
  229. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  230. $this->error(__('Captcha is incorrect'));
  231. }
  232. if (!in_array($gender, [1, 0])) {
  233. $this->error('性别错误');
  234. }
  235. $user = \app\common\model\User::getByMobile($mobile);
  236. if ($user) {
  237. $this->error('账号已经存在,请直接登录');
  238. if ($user->status == -1) {
  239. $this->error('账户已注销');
  240. }
  241. if (!in_array($user->status,[1,2])) {
  242. $this->error(__('Account is locked'));
  243. }
  244. //如果已经有账号则直接登录
  245. $ret = $this->auth->direct($user->id);
  246. } else {
  247. $extend = [
  248. 'register_from' => input('register_from',''),
  249. 'gender' => $gender
  250. ];
  251. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  252. }
  253. if ($ret) {
  254. Sms::flush($mobile, 'mobilelogin');
  255. $data = $this->userInfo('return');
  256. $this->success(__('Logged in successful'), $data);
  257. } else {
  258. $this->error($this->auth->getError());
  259. }
  260. }*/
  261. /**
  262. * 运营商一键登录
  263. */
  264. /*public function onLogin()
  265. {
  266. $accessToken = input('accessToken');// 运营商预取号获取到的token
  267. $token = input('tokenT');// 易盾返回的token
  268. if (!$accessToken || !$token) {
  269. $this->error("参数获取失败!");
  270. }
  271. $params = array(
  272. // 运营商预取号获取到的token
  273. "accessToken" => $accessToken,
  274. // 易盾返回的token
  275. "token" => $token
  276. );
  277. // 获取密钥配置
  278. $configInfo = config("onLogin");
  279. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  280. $onret = $onlogin->check($params);
  281. // $ret = [];
  282. // $ret["code"] = 200;
  283. // $ret["msg"] = "ok";
  284. // $ret["data"] = [
  285. // "phone" => "17574504021",
  286. // "resultCode" => 0
  287. // ];
  288. if ($onret["code"] == 200) {
  289. $mobile = $onret["data"]["phone"];
  290. if (empty($mobile)) {
  291. // 取号失败,建议进行二次验证,例如短信验证码
  292. $this->error("取号登录失败,请用验证码方式登录!");
  293. } else {
  294. // 取号成功, 执行登录等流程
  295. // 用户登录逻辑 === 开始
  296. $user = \app\common\model\User::getByMobile($mobile);
  297. if ($user) {
  298. if (!in_array($user->status,[1,2])) {
  299. $this->error(__('Account is locked'));
  300. }
  301. if ($user->frozentime > time()) {
  302. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  303. }
  304. //如果已经有账号则直接登录
  305. $ret = $this->auth->direct($user->id);
  306. $is_register = 0;
  307. } else {
  308. $extend = [
  309. 'register_from' => input('register_from',''),
  310. 'gender' => -1
  311. ];
  312. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  313. $is_register = 1;
  314. }
  315. if ($ret) {
  316. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  317. } else {
  318. $this->error($this->auth->getError());
  319. }
  320. // 用户登录逻辑 === 结束
  321. }
  322. } else {
  323. $this->error("登录失败,请用验证码方式登录!");
  324. }
  325. }*/
  326. /**
  327. * 运营商一键登录注册
  328. */
  329. /*public function onregister()
  330. {
  331. $accessToken = input('accessToken');// 运营商预取号获取到的token
  332. $token = input('tokenT');// 易盾返回的token
  333. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  334. if (!$accessToken || !$token) {
  335. $this->error("参数获取失败!");
  336. }
  337. if (!in_array($gender, [1, 0])) {
  338. $this->error('性别错误');
  339. }
  340. $params = array(
  341. // 运营商预取号获取到的token
  342. "accessToken" => $accessToken,
  343. // 易盾返回的token
  344. "token" => $token
  345. );
  346. // 获取密钥配置
  347. $configInfo = config("onLogin");
  348. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  349. $onret = $onlogin->check($params);
  350. // $ret = [];
  351. // $ret["code"] = 200;
  352. // $ret["msg"] = "ok";
  353. // $ret["data"] = [
  354. // "phone" => "17574504021",
  355. // "resultCode" => 0
  356. // ];
  357. if ($onret["code"] == 200) {
  358. $mobile = $onret["data"]["phone"];
  359. if (empty($mobile)) {
  360. // 取号失败,建议进行二次验证,例如短信验证码
  361. $this->error("取号登录失败,请用验证码方式登录!");
  362. } else {
  363. // 取号成功, 执行登录等流程
  364. // 用户登录逻辑 === 开始
  365. $user = \app\common\model\User::getByMobile($mobile);
  366. if ($user) {
  367. $this->error('账号已经存在,请直接登录');
  368. if (!in_array($user->status,[1,2])) {
  369. $this->error(__('Account is locked'));
  370. }
  371. //如果已经有账号则直接登录
  372. $ret = $this->auth->direct($user->id);
  373. $is_register = 0;
  374. } else {
  375. $extend = [
  376. 'register_from' => input('register_from',''),
  377. 'gender' => $gender
  378. ];
  379. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  380. $is_register = 1;
  381. }
  382. //结果
  383. if ($ret) {
  384. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  385. } else {
  386. $this->error($this->auth->getError());
  387. }
  388. // 用户登录逻辑 === 结束
  389. }
  390. } else {
  391. $this->error("登录失败,请用验证码方式登录!");
  392. }
  393. }*/
  394. //用户详细资料
  395. public function userInfo($type = 1){
  396. $info = $this->auth->getUserinfo();
  397. if($type == 'return'){
  398. return $info;
  399. }
  400. $this->success(__('success'),$info);
  401. }
  402. //微信登录,预先假注册
  403. /*public function wechatlogin(){
  404. $code = input('code','');
  405. if(!$code){
  406. $this->error(__('Invalid parameters'));
  407. }
  408. //微信
  409. $wechat = new Wechat();
  410. $wxuserinfo = $wechat->getAccessToken($code);
  411. if(!$wxuserinfo){
  412. $this->error('openid获取失败');
  413. }
  414. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  415. $this->error('openid获取失败');
  416. }
  417. $openid = $wxuserinfo['openid'];
  418. //检查用户
  419. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  420. if ($user) {
  421. if ($user['status'] == -1) {
  422. $this->error('账户已注销');
  423. }
  424. if ($user['status'] != 1) {
  425. $this->error(__('Account is locked'));
  426. }
  427. //如果已经有账号则直接登录
  428. $ret = $this->auth->direct($user['id']);
  429. if ($ret) {
  430. $userInfo = $this->auth->getUserinfo();
  431. $userInfo['is_register'] = 0;
  432. $userInfo['code'] = $code;
  433. $this->success(__('Logged in successful'), $userInfo);
  434. } else {
  435. $this->error($this->auth->getError());
  436. }
  437. } else {
  438. //记录code和openid,绑定手机号的时候更新openid
  439. $wechatCodeData = [
  440. 'code' => $code,
  441. 'openid' => $openid,
  442. 'createtime' => time(),
  443. ];
  444. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  445. if (empty($wechatCode)) {
  446. Db::name('wechat_code')->insertGetId($wechatCodeData);
  447. } else {
  448. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  449. }
  450. //直接返回
  451. $userInfo = [];
  452. $userInfo['is_register'] = 1;
  453. $userInfo['code'] = $code;
  454. $this->success('获取信息成功', $userInfo);
  455. }
  456. }*/
  457. /**
  458. * 微信注册来的,绑定手机号
  459. *
  460. * @ApiMethod (POST)
  461. * @param string $mobile 手机号
  462. * @param string $captcha 验证码
  463. */
  464. /*public function wechatbindmobile()
  465. {
  466. $mobile = $this->request->param('mobile');
  467. $captcha = $this->request->param('captcha');
  468. $code = $this->request->param('code');
  469. if (!$mobile || !$captcha || !$code) {
  470. $this->error(__('Invalid parameters'));
  471. }
  472. if (!Validate::regex($mobile, "^1\d{10}$")) {
  473. $this->error(__('Mobile is incorrect'));
  474. }
  475. $result = Sms::check($mobile, $captcha, 'changemobile');
  476. if (!$result) {
  477. $this->error(__('Captcha is incorrect'));
  478. }
  479. $wechatCodeWhere['code'] = $code;
  480. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  481. if (empty($wechatCode)) {
  482. $this->error('请先微信登录');
  483. }
  484. //检查appid绑定的用户
  485. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  486. if ($user) {
  487. if ($user['status'] == -1) {
  488. $this->error('账户已注销');
  489. }
  490. if ($user['status'] != 1) {
  491. $this->error(__('Account is locked'));
  492. }
  493. //如果已经有账号则直接登录
  494. $ret = $this->auth->direct($user['id']);
  495. $this->success('success',$this->auth->getUserinfo());
  496. }
  497. //新的openid用户
  498. $where = [];
  499. $where['mobile'] = $mobile;
  500. $userData = Db::name('user')->where($where)->find();//老用户
  501. if (!empty($userData)) {
  502. if (empty($userData['wechat_openid'])) {
  503. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  504. } else {
  505. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  506. $this->error('该手机号已被其他用户绑定');
  507. }
  508. }
  509. $ret = $this->auth->direct($userData['id']);
  510. } else {
  511. $extend = [
  512. 'wechat_openid' => $wechatCode['openid'],
  513. ];
  514. $ret = $this->auth->register('', '','', $mobile, $extend);
  515. }
  516. if (!$ret) {
  517. $this->error($this->auth->getError());
  518. }
  519. $this->success('success',$this->auth->getUserinfo());
  520. }*/
  521. public function testhaibao(){
  522. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
  523. // dump($haibao);
  524. $this->success('success', $haibao);
  525. }
  526. //海报
  527. public function haibao($player_id,$data){
  528. //下载页二维码,没必要保留
  529. $params = [
  530. 'text' => config('site.domain_name').'?code=' . $data['introcode'],
  531. 'size' => 90,
  532. 'logo' => false,
  533. 'label' => false,
  534. 'padding' => 0,
  535. ];
  536. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  537. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  538. mk_dir($qrcode_path);
  539. $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
  540. $qrCode->writeFile($download_qrcode);
  541. //海报
  542. $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
  543. return $haibao;
  544. }
  545. /*public function createhaibao($download_qrcode,$player_id,$sub_data){
  546. //背景图
  547. $background = $sub_data['background'] ? $sub_data['background'] : config('site.domain_name').'/assets/img/haibao.png';
  548. //海报图片路径
  549. $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
  550. mk_dir($new_path);
  551. $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
  552. //二维码
  553. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  554. //合成wap图片
  555. $image = new \addons\poster\library\Image2();
  556. $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
  557. return '/'.$wap_file_name;
  558. }*/
  559. public function createhaibao($download_qrcode,$player_id,$sub_data){
  560. //二维码
  561. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  562. $data = [
  563. [
  564. "left" => "15px",
  565. "top" => "296px",
  566. "type" => "img",
  567. "width" => "58px",
  568. "height" => "58px",
  569. "src" => one_domain_image($this->auth->avatar)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  570. ],
  571. [
  572. "left" => "81px",
  573. "top" => "300px",
  574. "type" => "nickname",
  575. "width" => "80px",
  576. "height" => "24px",
  577. "size" => "12px",
  578. "color" => "#000",
  579. "content" => $this->auth->nickname
  580. ],
  581. [
  582. "left" => "81px",
  583. "top" => "327px",
  584. "type" => "nickname",
  585. "width" => "80px",
  586. "height" => "24px",
  587. "size" => "12px",
  588. "color" => "#000",
  589. "content" => $this->auth->introcode
  590. ],
  591. [
  592. "left" => "227px",
  593. "top" => "283px",
  594. "type" => "img",
  595. "width" => "80px",
  596. "height" => "80px",
  597. // "src" => httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  598. "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  599. ]
  600. ];
  601. $data = json_encode($data, 320);
  602. $poster = [
  603. 'id' => 1,
  604. 'title' => '测试2',
  605. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  606. 'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
  607. 'data' => $data,
  608. 'status' => 'normal',
  609. 'weigh' => 0,
  610. 'createtime' => 1653993709,
  611. 'updatetime' => 1653994259,
  612. ];
  613. $image = new \addons\poster\library\Image();
  614. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  615. if (!$imgurl) {
  616. $this->error('生成海报出错');
  617. }
  618. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  619. return '/' . $imgurl;
  620. }
  621. //实名认证信息
  622. public function idcard_confirm_info(){
  623. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  624. if (!$check) {
  625. $check = (object)[];
  626. }
  627. $this->success('success',$check);
  628. }
  629. /**
  630. * 退出登录
  631. * @ApiMethod (POST)
  632. */
  633. public function logout()
  634. {
  635. if (!$this->request->isPost()) {
  636. $this->error(__('Invalid parameters'));
  637. }
  638. //退出im
  639. $tenIm = new Tenim();
  640. $tenIm->loginoutim($this->auth->id);
  641. //修改用户活跃0
  642. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  643. $this->auth->logout();
  644. $this->success(__('Logout successful'));
  645. }
  646. /**
  647. * 修改会员个人信息
  648. *
  649. * @ApiMethod (POST)
  650. * @param string $avatar 头像地址
  651. * @param string $username 用户名
  652. * @param string $nickname 昵称
  653. * @param string $bio 个人简介
  654. */
  655. public function profile()
  656. {
  657. $field_array = ['nickname','introcode',/*'gender',*/'birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo','wechat_account','character_id','constellation_id','stature_id','is_appointment', 'greet_voice', 'greet_chat', 'is_cohabit', 'live_id', 'is_house', 'car_id', 'chest_id', 'waist'];
  658. $data = [];
  659. foreach($field_array as $key => $field){
  660. if(!input('?'.$field)){
  661. continue;
  662. }
  663. $newone = input($field);
  664. if($field == 'avatar'){
  665. // if ($this->auth->real_status == 1) {
  666. //$this->error('您已经真人认证不能修改头像~');
  667. // die;
  668. // }
  669. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  670. }
  671. if($field == 'photo_images'){
  672. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  673. }
  674. $data[$field] = $newone;
  675. }
  676. if(isset($data['birthday'])){
  677. $data['birthday'] = strtotime($data['birthday']);
  678. }
  679. if(isset($data['avatar'])){
  680. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  681. }
  682. if(isset($data['hobby_ids'])){
  683. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  684. }
  685. if(isset($data['tag_ids'])){
  686. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  687. }
  688. if(isset($data['introcode'])){
  689. if ($this->auth->intro_uid != 0) {
  690. $this->error('邀请人不可修改~');
  691. }
  692. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $data['introcode'])->find();
  693. if(!$intro_user){
  694. $this->error('不存在的邀请人');
  695. }
  696. if ($intro_user['id'] == $this->auth->id) {
  697. $this->error('不能填写自己邀请码');
  698. }
  699. if ($intro_user['intro_uid'] == $this->auth->id) {
  700. $this->error('不能填写下级邀请码');
  701. }
  702. unset($data['introcode']);//别人的邀请码,不能改了自己的
  703. $data['intro_uid'] = $intro_user['id'];
  704. $data['invite_time'] = time();
  705. }
  706. //dump($data);
  707. if(empty($data)){
  708. $this->error('没有任何改变');
  709. }
  710. Db::startTrans();
  711. $userData = Db::name('user')->field('avatar,gender,status')->where('id',$this->auth->id)->find();
  712. if (isset($data['avatar']) && !empty($data['avatar']) && $userData['status'] == 2) {//隐藏
  713. $boyAvatar = config('avatar_boy');
  714. $girlAvatar = config('avatar_girl');
  715. if ($userData['gender'] == 1 && $data['avatar'] != $boyAvatar) {
  716. $data['status'] = 1;//更新为正常
  717. } elseif ($userData['gender'] == 0 && $data['avatar'] != $girlAvatar) {
  718. $data['status'] = 1;//更新为正常
  719. }
  720. }
  721. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  722. if($update_rs === false){
  723. Db::rollback();
  724. $this->error('修改资料失败');
  725. }
  726. //给上级发放钻石
  727. if(isset($data['intro_uid'])){
  728. $intro_jewel = config('site.new_user_intro_jewel');
  729. if($intro_jewel > 0){
  730. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'jewel',$intro_jewel,34,'邀请'.$this->auth->username.'注册奖励');
  731. if($rs_wallet['status'] === false){
  732. Db::rollback();
  733. $this->error('邀请新人奖励赠送失败');
  734. }
  735. }
  736. }
  737. //tag任务赠送金币
  738. //上传头像加5金币
  739. if(isset($data['avatar'])){
  740. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  741. if($task_rs === false){
  742. Db::rollback();
  743. $this->error('完成任务赠送奖励失败');
  744. }
  745. }
  746. //上传生日 +5金币
  747. if (isset($data['birthday'])) {
  748. //完成设置生日 +5金币
  749. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  750. if($task_rs === false){
  751. Db::rollback();
  752. $this->error('完成任务赠送奖励失败');
  753. }
  754. }
  755. //上传个性签名 5金币
  756. if(isset($data['audio_bio'])){
  757. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,2);
  758. if($task_rs === false){
  759. Db::rollback();
  760. $this->error('完成任务赠送奖励失败');
  761. }
  762. }
  763. //上传本人语音介绍 10金币
  764. if(isset($data['audio_bio'])){
  765. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,21);
  766. if($task_rs === false){
  767. Db::rollback();
  768. $this->error('完成任务赠送奖励失败');
  769. }
  770. }
  771. //上传本人五张照片 10金币
  772. if(isset($data['photo_images'])){
  773. $photo_images_num = count(explode(',',$data['photo_images'])) + count(explode(',',$this->auth->photo_images));
  774. if ($photo_images_num >= 5) {
  775. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id, 5);
  776. if ($task_rs === false) {
  777. Db::rollback();
  778. $this->error('完成任务赠送奖励失败');
  779. }
  780. }
  781. }
  782. Db::commit();
  783. $this->success();
  784. }
  785. /*
  786. * 修改用户的坐标
  787. * */
  788. public function change_longlat(){
  789. $longitude = input_post('longitude');
  790. $latitude = input_post('latitude');
  791. $cityname = input_post('cityname');
  792. if(empty($longitude) || empty($latitude) || empty($cityname)){
  793. $this->error();
  794. }
  795. $data = [
  796. 'longitude' => $longitude,
  797. 'latitude' => $latitude,
  798. 'cityname' => $cityname,
  799. ];
  800. Db::name('user')->where('id',$this->auth->id)->update($data);
  801. $this->success();
  802. }
  803. /**
  804. * 修改手机号
  805. *
  806. * @ApiMethod (POST)
  807. * @param string $mobile 手机号
  808. * @param string $captcha 验证码
  809. */
  810. public function changemobile()
  811. {
  812. $user = $this->auth->getUser();
  813. $oldcaptcha = $this->request->request('oldcaptcha');
  814. $mobile = $this->request->request('mobile');
  815. $captcha = $this->request->request('captcha');
  816. if (!$oldcaptcha || !$mobile || !$captcha) {
  817. $this->error(__('Invalid parameters'));
  818. }
  819. if (!Validate::regex($mobile, "^1\d{10}$")) {
  820. $this->error(__('Mobile is incorrect'));
  821. }
  822. if($user->mobile == $mobile){
  823. $this->error('新手机号不能与旧手机号相同');
  824. }
  825. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  826. $this->error(__('Mobile already exist'));
  827. }
  828. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  829. if (!$result) {
  830. $this->error(__('Captcha is incorrect'));
  831. }
  832. $result = Sms::check($mobile, $captcha, 'changemobile');
  833. if (!$result) {
  834. $this->error(__('Captcha is incorrect'));
  835. }
  836. $verification = $user->verification;
  837. $verification->mobile = 1;
  838. $user->verification = $verification;
  839. $user->mobile = $mobile;
  840. $user->save();
  841. Sms::flush($user->mobile, 'changemobile');
  842. Sms::flush($mobile, 'changemobile');
  843. $this->success();
  844. }
  845. /**
  846. * 微信注册来的,绑定手机号
  847. *
  848. * @ApiMethod (POST)
  849. * @param string $mobile 手机号
  850. * @param string $captcha 验证码
  851. */
  852. /*public function bindmobile()
  853. {
  854. $user = $this->auth->getUser();
  855. $mobile = $this->request->request('mobile');
  856. $captcha = $this->request->request('captcha');
  857. if(!empty($this->auth->mobile)){
  858. $this->error('已经绑定了手机号');
  859. }
  860. if (!$mobile || !$captcha) {
  861. $this->error(__('Invalid parameters'));
  862. }
  863. if (!Validate::regex($mobile, "^1\d{10}$")) {
  864. $this->error(__('Mobile is incorrect'));
  865. }
  866. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  867. $this->error('该手机号已被其他用户绑定');
  868. }
  869. $result = Sms::check($mobile, $captcha, 'changemobile');
  870. if (!$result) {
  871. $this->error(__('Captcha is incorrect'));
  872. }
  873. $verification = $user->verification;
  874. $verification->mobile = 1;
  875. $user->verification = $verification;
  876. $user->mobile = $mobile;
  877. $user->save();
  878. Sms::flush($mobile, 'changemobile');
  879. //手机号奖励
  880. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,18);
  881. if($task_rs === false){
  882. Db::rollback();
  883. return false;
  884. }
  885. $this->success('success',$this->userInfo('return'));
  886. }*/
  887. /**
  888. * 手机号注册来的,绑定微信
  889. *
  890. * @ApiMethod (POST)
  891. * @param string wechat_openid 微信openid
  892. */
  893. /*public function bindopenid()
  894. {
  895. $user = $this->auth->getUser();
  896. $wechat_openid = $this->request->request('wechat_openid');
  897. if(!empty($this->auth->wechat_openid)){
  898. $this->error('已经绑定了微信号');
  899. }
  900. if (!$wechat_openid) {
  901. $this->error(__('Invalid parameters'));
  902. }
  903. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  904. $this->error('该微信号已被其他用户绑定');
  905. }
  906. $user->wechat_openid = $wechat_openid;
  907. $user->save();
  908. $this->success('success',$this->userInfo('return'));
  909. }*/
  910. /**
  911. * 修改密码
  912. *
  913. * @ApiMethod (POST)
  914. * @param string $newpassword 新密码
  915. * @param string $oldpassword 旧密码
  916. */
  917. public function changepwd(){
  918. $newpassword = input('newpassword');
  919. $oldpassword = input('oldpassword','');
  920. if (!$newpassword) {
  921. $this->error(__('Invalid parameters'));
  922. }
  923. if($this->auth->password && empty($oldpassword)){
  924. $this->error('原密码必填');
  925. }
  926. if(empty($this->auth->password)){
  927. $ret = $this->auth->changepwd($newpassword, '', true);
  928. }else{
  929. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  930. }
  931. if ($ret) {
  932. $this->success(__('Reset password successful'));
  933. } else {
  934. $this->error($this->auth->getError());
  935. }
  936. }
  937. /**
  938. * 记录当前登陆的设备ID,设备信息,IP等
  939. */
  940. public function changeDeviceIp()
  941. {
  942. // 接口防并发
  943. if (!$this->apiLimit(1, 5)) {
  944. return ;
  945. }
  946. $user = $this->auth->getUser();
  947. $ip = request()->ip();
  948. $deviceId = $this->request->request('device_id','');
  949. $phoneModel = $this->request->request('phone_model','');
  950. $brand = $this->request->request('brand','');
  951. $apiVersion = $this->request->request('api_version','');
  952. $deviceOs = $this->request->request('device_os','');
  953. if ($ip !== $user->loginip){
  954. $update = [];
  955. $update['id'] = $user->id;
  956. $update['loginip'] = $ip;
  957. \app\common\model\User::update($update);
  958. }
  959. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  960. if (empty($userDeviceInfo)){
  961. $userDeviceInfo = new UserDeviceInfo();
  962. $userDeviceInfo->user_id = $user->u_id;
  963. }
  964. $userDeviceInfo->device_os = $deviceOs;
  965. $userDeviceInfo->device_id = $deviceId;
  966. $userDeviceInfo->phone_model = $phoneModel;
  967. $userDeviceInfo->brand = $brand;
  968. $userDeviceInfo->api_version = $apiVersion;
  969. $userDeviceInfo->save();
  970. //首页接口调用,这里不反回信息
  971. // $this->success("更新成功!");
  972. }
  973. //假注销
  974. public function cancleUser(){
  975. if (!$this->request->isPost()) {
  976. $this->error(__('Invalid parameters'));
  977. }
  978. //退出im
  979. $tenIm = new Tenim();
  980. $tenIm->loginoutim($this->auth->id);
  981. $data = [
  982. 'status' => -1,
  983. 'mobile' => 'close_'.$this->auth->mobile,
  984. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  985. ];
  986. Db::name('user')->where('id',$this->auth->id)->update($data);
  987. $this->auth->logout();
  988. $this->success('注销成功');
  989. }
  990. //修改用户活跃1
  991. /*public function useractive(){
  992. $this->success('success');
  993. }*/
  994. //公众号获取openid
  995. /*public function getUserOpenid_gzh(){
  996. $configValue = Service::getConfig('wechat');
  997. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  998. $rs = $wechat->getOpenid();
  999. $this->success('success',$rs);
  1000. }*/
  1001. /**
  1002. * 微信内H5-JSAPI支付
  1003. */
  1004. /*public function jssdkBuildConfig() {
  1005. $url = $this->request->request("url");
  1006. $configValue = Service::getConfig('wechat');
  1007. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1008. $sign = $wechat->getSignPackage(urldecode($url));
  1009. $this->success("获取成功!",$sign);
  1010. }*/
  1011. //苹果账号登录
  1012. /*public function ioslogin(){
  1013. $ios_openid = input('ios_openid','');
  1014. if (!$ios_openid) {
  1015. $this->error(__('Invalid parameters'));
  1016. }
  1017. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1018. // if (!$user) {
  1019. // $this->success('选择性别', ['code' => 5]);
  1020. // }
  1021. if ($user) {
  1022. if (!in_array($user->status,[1,2])) {
  1023. $this->error(__('Account is locked'));
  1024. }
  1025. if ($user->frozentime > time()) {
  1026. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  1027. }
  1028. //如果已经有账号则直接登录
  1029. $ret = $this->auth->direct($user->id);
  1030. } else {
  1031. $reg_data = [
  1032. 'register_from' => input('register_from',''),
  1033. 'gender' => -1
  1034. ];
  1035. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1036. }
  1037. if ($ret) {
  1038. $data = $this->userInfo('return');
  1039. $this->success(__('Logged in successful'), $data);
  1040. } else {
  1041. $this->error($this->auth->getError());
  1042. }
  1043. }*/
  1044. //苹果账号注册
  1045. /*public function iosregiter(){
  1046. $ios_openid = input('ios_openid', '', 'trim');
  1047. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  1048. if (!$ios_openid) {
  1049. $this->error(__('Invalid parameters'));
  1050. }
  1051. if (!in_array($gender, [1, 0])) {
  1052. $this->error('性别错误');
  1053. }
  1054. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1055. if ($user) {
  1056. $this->error('账号已经存在,请直接登录');
  1057. }
  1058. $reg_data = [
  1059. 'register_from' => input('register_from',''),
  1060. 'gender' => $gender
  1061. ];
  1062. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1063. if ($ret) {
  1064. $data = $this->userInfo('return');
  1065. $this->success(__('Logged in successful'), $data);
  1066. } else {
  1067. $this->error($this->auth->getError());
  1068. }
  1069. }*/
  1070. //注册完善资料
  1071. public function perfect_info() {
  1072. $avatar = input('avatar', '', 'trim'); //头像
  1073. $nickname = input('nickname', '', 'trim'); //昵称
  1074. $birthday = input('birthday', '', 'strtotime'); //生日
  1075. $hometown_cityid = input('hometown_cityid', '', 'trim'); //城市id
  1076. $job_id = input('job_id', '', 'trim'); //职业id
  1077. $character_id = input('character_id', '', 'trim'); //性格id
  1078. $introcode = input('introcode', '', 'trim'); //邀请码
  1079. $data = [];
  1080. if ($avatar) {
  1081. $data['avatar'] = $avatar;
  1082. }
  1083. if ($nickname !== '') {
  1084. if (iconv_strlen($nickname, 'utf-8') > 10) {
  1085. $this->error('昵称最多10个字~');
  1086. }
  1087. $data['nickname'] = $nickname;
  1088. }
  1089. if ($birthday) {
  1090. $data['birthday'] = $birthday;
  1091. }
  1092. if ($hometown_cityid) {
  1093. $count = Db::name('area')->where('id', $hometown_cityid)->count('id');
  1094. if (!$count) {
  1095. $this->error('城市不存在');
  1096. }
  1097. $data['hometown_cityid'] = $hometown_cityid;
  1098. }
  1099. if ($job_id) {
  1100. $count = Db::name('enum_job')->where('id', $job_id)->count('id');
  1101. if (!$count) {
  1102. $this->error('职业不存在');
  1103. }
  1104. $data['job_id'] = $job_id;
  1105. }
  1106. if ($character_id) {
  1107. $count = Db::name('enum_character')->where('id', $character_id)->count('id');
  1108. if (!$count) {
  1109. $this->error('性格不存在');
  1110. }
  1111. $data['character_id'] = $character_id;
  1112. }
  1113. if ($introcode && !$this->auth->intro_uid) {
  1114. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $introcode)->find();
  1115. if ($intro_user && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id) {
  1116. $data['intro_uid'] = $intro_user['id'];
  1117. $data['invite_time'] = time();
  1118. }
  1119. }
  1120. //开启事务
  1121. Db::startTrans();
  1122. $update_rs = Db::name('user')->where('id',$this->auth->id)->setField($data);
  1123. if($update_rs === false){
  1124. Db::rollback();
  1125. $this->error('修改失败');
  1126. }
  1127. //给上级发放钻石
  1128. if(isset($data['intro_uid'])){
  1129. $intro_jewel = config('site.new_user_intro_jewel');
  1130. if($intro_jewel > 0){
  1131. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'jewel',$intro_jewel,34,'邀请'.$this->auth->username.'注册奖励');
  1132. if($rs_wallet['status'] === false){
  1133. Db::rollback();
  1134. $this->error('邀请新人奖励赠送失败');
  1135. }
  1136. }
  1137. }
  1138. //上传头像加5金币
  1139. if(isset($data['avatar'])){
  1140. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  1141. if($task_rs === false){
  1142. Db::rollback();
  1143. $this->error('完成任务赠送奖励失败');
  1144. }
  1145. }
  1146. if (isset($data['birthday'])) {
  1147. //完成设置生日 +5金币
  1148. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  1149. if($task_rs === false){
  1150. Db::rollback();
  1151. $this->error('完成任务赠送奖励失败');
  1152. }
  1153. }
  1154. Db::commit();
  1155. $this->success('修改成功');
  1156. }
  1157. //实名认证
  1158. public function idcard_auth() {
  1159. $info = Db::name('user_idconfirm')->where(['user_id' => $this->auth->id])->find();
  1160. // if ($info && $info['status'] == 0) {
  1161. // $this->error('您已经提交信息了,请进行人脸认证!');
  1162. // }
  1163. if ($info && $info['status'] == 1) {
  1164. $this->error('您已通过审核!');
  1165. }
  1166. $nickname = input('nickname', '', 'trim'); // 姓名
  1167. $idcard = input('idcard', '', 'trim'); // 身份证号
  1168. if ($nickname === '') {
  1169. $this->error('请输入姓名');
  1170. }
  1171. if (iconv_strlen($nickname, 'utf-8') > 50) {
  1172. $this->error('请输入正确姓名');
  1173. }
  1174. if ($idcard === '') {
  1175. $this->error('请输入身份证号');
  1176. }
  1177. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1178. $this->error('请输入正确身份证号');
  1179. }
  1180. $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  1181. if ($count) {
  1182. $this->error('身份证号已存在');
  1183. }
  1184. $data = [];
  1185. $data['truename'] = $nickname;
  1186. $data['idcard'] = $idcard;
  1187. //腾讯云身份证二要素认证
  1188. $auth_restult = $this->userauth_tencent($idcard, $nickname);
  1189. if ($auth_restult) {
  1190. $data['status'] = 1; //通过
  1191. $msg = '认证通过';
  1192. } else {
  1193. $data['status'] = 2; //不通过
  1194. $msg = '认证不通过';
  1195. }
  1196. //开启事务
  1197. Db::startTrans();
  1198. if (!$info) { //未认证
  1199. $data["user_id"] = $this->auth->id;
  1200. $data["createtime"] = time();
  1201. $res = Db::name('user_idconfirm')->insertGetId($data);
  1202. } else { //认证被拒绝过
  1203. $data['updatetime'] = time();
  1204. $res = Db::name('user_idconfirm')->where(['id' => $info['id'], 'user_id' => $this->auth->id])->setField($data);
  1205. }
  1206. if (!$res) {
  1207. Db::rollback();
  1208. $this->error('认证失败');
  1209. }
  1210. $rt = Db::name('user')->where(['id' => $this->auth->id, 'idcard_status' => $this->auth->idcard_status])->setField('idcard_status', $data['status']);
  1211. if ($rt === false) {
  1212. Db::rollback();
  1213. $this->error('认证失败');
  1214. }
  1215. if ($data['status'] == 1) {
  1216. //完成实名认证 +20金币
  1217. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  1218. if($task_rs === false){
  1219. Db::rollback();
  1220. $this->error('完成任务赠送奖励失败');
  1221. }
  1222. //系统消息
  1223. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证已经审核通过');
  1224. } else {
  1225. //系统消息
  1226. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证审核不通过');
  1227. }
  1228. Db::commit();
  1229. $this->success($msg);
  1230. }
  1231. //腾讯云身份证二要素认证
  1232. public function userauth_tencent($idcard = '', $nickname = '') {
  1233. // require_once 'vendor/autoload.php';
  1234. try {
  1235. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1236. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1237. $config = config('tencent_im');
  1238. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1239. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1240. $httpProfile = new HttpProfile();
  1241. $httpProfile->setEndpoint("faceid.tencentcloudapi.com");
  1242. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1243. $clientProfile = new ClientProfile();
  1244. $clientProfile->setHttpProfile($httpProfile);
  1245. // 实例化要请求产品的client对象,clientProfile是可选的
  1246. $client = new FaceidClient($cred, "", $clientProfile);
  1247. // 实例化一个请求对象,每个接口都会对应一个request对象
  1248. $req = new IdCardVerificationRequest();
  1249. $params = array(
  1250. "IdCard" => $idcard,
  1251. "Name" => $nickname
  1252. );
  1253. $req->fromJsonString(json_encode($params));
  1254. // 返回的resp是一个IdCardVerificationResponse的实例,与请求对象对应
  1255. $resp = $client->IdCardVerification($req);
  1256. // 输出json格式的字符串回包
  1257. // print_r($resp->toJsonString());
  1258. $result = json_decode($resp->toJsonString(), true);
  1259. if (isset($result['Result']) && $result['Result'] == 0) {
  1260. return 1; //通过
  1261. } else {
  1262. return 0;
  1263. }
  1264. }
  1265. catch(TencentCloudSDKException $e) {
  1266. // echo $e;
  1267. return 0;
  1268. }
  1269. }
  1270. //绑定支付宝
  1271. public function addalipayaccount() {
  1272. $id = input('id', 0, 'intval'); //账号id
  1273. $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
  1274. // $type = 1;
  1275. $realname = input('realname', '', 'trim'); //账户真实姓名
  1276. $banknumber = input('banknumber', '', 'trim'); //卡号或账号
  1277. $bankname = input('bankname', '', 'trim'); //银行名称
  1278. // if ($id) {
  1279. // $this->error('您已经拥有该类型账号,请联系后台解绑');
  1280. // }
  1281. if (!in_array($type, [1, 2])) {
  1282. $this->error('参数错误');
  1283. }
  1284. /*if ($type == 1) {
  1285. $this->error('暂不支持绑定支付宝');
  1286. }*/
  1287. if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
  1288. $this->error('账号姓名1-30位');
  1289. }
  1290. if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
  1291. $this->error('账号1-50位');
  1292. }
  1293. if ($type == 1) {
  1294. if (iconv_strlen($bankname, 'utf-8') != 18) {
  1295. $this->error('请输入正确身份证号');
  1296. }
  1297. }
  1298. if ($type == 2) {
  1299. if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
  1300. $this->error('开户行名称1-50位');
  1301. }
  1302. }
  1303. $user_bank = Db::name('user_bank');
  1304. if ($id) {
  1305. //编辑
  1306. //查询账号是否存在
  1307. $info = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1308. if (!$info) {
  1309. $this->error('账号不存在');
  1310. }
  1311. if ($info['type'] != $type) {
  1312. $this->error('账号类型错误');
  1313. }
  1314. //查询是否未通过提现记录
  1315. $count = Db::name('take_cash')->where(['user_id' => $this->auth->id, 'alipay_account' => $info['banknumber'], 'status' => 0])->count('id');
  1316. if ($count) {
  1317. $this->error('该账户有未审核的提现记录,暂不可修改');
  1318. }
  1319. $data['realname'] = $realname;
  1320. $data['banknumber'] = $banknumber;
  1321. $data['updatetime'] = time();
  1322. // if ($type == 2) {
  1323. $data['bankname'] = $bankname;
  1324. // }
  1325. $rs = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->setField($data);
  1326. if ($rs === false) {
  1327. $this->error('修改失败');
  1328. }
  1329. $this->success('修改成功');
  1330. } else {
  1331. //添加
  1332. //查询是否已经拥有该类型账号
  1333. $count = $user_bank->where(['user_id' => $this->auth->id, 'type' => $type])->count('id');
  1334. if ($count) {
  1335. $this->error('您已经拥有该类型账号,请联系后台解绑');
  1336. }
  1337. $data['user_id'] = $this->auth->id;
  1338. $data['type'] = $type;
  1339. $data['realname'] = $realname;
  1340. $data['banknumber'] = $banknumber;
  1341. // if ($type == 2) {
  1342. $data['bankname'] = $bankname;
  1343. // }
  1344. $data['createtime'] = time();
  1345. $rs = $user_bank->insertGetId($data);
  1346. if (!$rs) {
  1347. $this->error('添加失败');
  1348. }
  1349. $this->success('添加成功');
  1350. }
  1351. }
  1352. //查询支付宝/银行卡信息
  1353. public function alipayinfo() {
  1354. $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
  1355. $info = Db::name('user_bank')->field('id, realname, banknumber, bankname')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  1356. if (!$info) {
  1357. $info = (object)[];
  1358. }
  1359. $this->success('success', $info);
  1360. }
  1361. //申请真人认证
  1362. public function realauth() {
  1363. if ($this->auth->real_status == 1) {
  1364. $this->error('您已经真人认证过了~');
  1365. }
  1366. if ($this->auth->avatar == config('avatar_boy') || $this->auth->avatar == config('avatar_girl')) {
  1367. $this->error('请先上传真人头像~');
  1368. }
  1369. //获取token
  1370. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.config('tencent_yun')['secret_id'].'&secret='.config('tencent_yun')['secret_key'].'&grant_type=client_credential&version=1.0.0';
  1371. $token_result = file_get_contents($token_url);
  1372. if (!$token_result) {
  1373. $this->error('您的网络开小差啦1~');
  1374. }
  1375. $token_result = json_decode($token_result, true);
  1376. if ($token_result['code'] != 0) {
  1377. $this->error('您的网络开小差啦2~');
  1378. }
  1379. $token = $token_result['access_token'];
  1380. //获取签名鉴权参数ticket
  1381. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.config('tencent_yun')['secret_id'].'&access_token='.$token.'&type=SIGN&version=1.0.0';
  1382. $ticket_result = file_get_contents($ticket_url);
  1383. if (!$ticket_result) {
  1384. $this->error('您的网络开小差啦3~');
  1385. }
  1386. $ticket_result = json_decode($ticket_result, true);
  1387. if ($ticket_result['code'] != 0) {
  1388. $this->error('您的网络开小差啦4~');
  1389. }
  1390. $ticket = $ticket_result['tickets'][0]['value'];
  1391. //获取签名
  1392. $sign_data = [
  1393. 'wbappid' => config('tencent_yun')['secret_id'],
  1394. 'userId' => (string)$this->auth->id,
  1395. 'version' => '1.0.0',
  1396. 'ticket' => $ticket,
  1397. 'nonce' => Random::alnum(32)
  1398. ];//p($sign_data);
  1399. asort($sign_data); //p($sign_data);//排序
  1400. $sign_string = join('', $sign_data);//p($sign_string);
  1401. $sign = sha1($sign_string);//p($sign);
  1402. //上传身份信息
  1403. $orderNo = getMillisecond() . $this->auth->id . mt_rand(1, 1000); //商户请求的唯一标识
  1404. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo=' . $orderNo;
  1405. $avatar = one_domain_image($this->auth->avatar);
  1406. $avatar = str_replace('https', 'http', $avatar);
  1407. $img = file_get_contents($avatar);
  1408. $img = str_replace('data:image/jpg;base64', '', $img);
  1409. $img = str_replace('\n', '', $img);
  1410. $sourcePhotoStr = base64_encode($img);
  1411. $data = [
  1412. 'webankAppId' => config('tencent_yun')['secret_id'],
  1413. 'orderNo' => $orderNo,
  1414. 'userId' => (string)$this->auth->id,
  1415. 'sourcePhotoStr' => $sourcePhotoStr,
  1416. 'sourcePhotoType' => 2,
  1417. 'version' => '1.0.0',
  1418. 'sign' => $sign,
  1419. 'nonce' => $sign_data['nonce']
  1420. ];
  1421. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1422. if (!$rs) {
  1423. $this->error('您的网络开小差啦5~');
  1424. }
  1425. $rs = json_decode($rs, true);
  1426. if (!$rs || $rs['code'] != 0) {
  1427. $this->error('您的网络开小差啦6~');
  1428. }
  1429. $user_auth = [
  1430. 'user_id' => $this->auth->id,
  1431. 'certify_id' => $rs['result']['faceId'],
  1432. 'out_trade_no' => $data['orderNo'],
  1433. 'status' => 0,
  1434. 'createtime' => time(),
  1435. 'updatetime' => time()
  1436. ];
  1437. //开启事务
  1438. Db::startTrans();
  1439. //查询是否认证过
  1440. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1441. if ($info) {
  1442. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  1443. } else {
  1444. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  1445. }
  1446. if (!$auth_rs) {
  1447. Db::rollback();
  1448. $this->error('您的网络开小差啦7~');
  1449. }
  1450. //修改用户表认证状态
  1451. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', 0);
  1452. if ($user_rs === false) {
  1453. Db::rollback();
  1454. $this->error('您的网络开小差啦8~');
  1455. }
  1456. Db::commit();
  1457. $return_data = [
  1458. 'face_id' => $user_auth['certify_id'],
  1459. 'order_no' => $user_auth['out_trade_no'],
  1460. 'user_id' => (string)$this->auth->id,
  1461. 'nonce' => $sign_data['nonce'],
  1462. 'sign' => $sign
  1463. ];
  1464. $this->success('success', $return_data);
  1465. }
  1466. //查询真人认证结果
  1467. public function getrealauthresult() {
  1468. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1469. if (!$user_auth) {
  1470. $this->success('尚未认证');
  1471. }
  1472. if ($user_auth['status'] == 1) {
  1473. $this->success('真人认证通过');
  1474. }
  1475. if (!$user_auth['certify_id']) {
  1476. $this->success('请先进行真人认证');
  1477. }
  1478. //获取token
  1479. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.config('tencent_yun')['secret_id'].'&secret='.config('tencent_yun')['secret_key'].'&grant_type=client_credential&version=1.0.0';
  1480. $token_result = file_get_contents($token_url);
  1481. if (!$token_result) {
  1482. $this->error('您的网络开小差啦1~');
  1483. }
  1484. $token_result = json_decode($token_result, true);
  1485. if ($token_result['code'] != 0) {
  1486. $this->error('您的网络开小差啦2~');
  1487. }
  1488. $token = $token_result['access_token'];
  1489. //获取签名鉴权参数ticket
  1490. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.config('tencent_yun')['secret_id'].'&access_token='.$token.'&type=SIGN&version=1.0.0';
  1491. $ticket_result = file_get_contents($ticket_url);
  1492. if (!$ticket_result) {
  1493. $this->error('您的网络开小差啦3~');
  1494. }
  1495. $ticket_result = json_decode($ticket_result, true);
  1496. if ($ticket_result['code'] != 0) {
  1497. $this->error('您的网络开小差啦4~');
  1498. }
  1499. $ticket = $ticket_result['tickets'][0]['value'];
  1500. //获取签名
  1501. $sign_data = [
  1502. 'wbappid' => config('tencent_yun')['secret_id'],
  1503. 'orderNo' => $user_auth['out_trade_no'],
  1504. 'version' => '1.0.0',
  1505. 'ticket' => $ticket,
  1506. 'nonce' => Random::alnum(32)
  1507. ];//p($sign_data);
  1508. asort($sign_data); //p($sign_data);//排序
  1509. $sign_string = join('', $sign_data);//p($sign_string);
  1510. $sign = sha1($sign_string);//p($sign);
  1511. //人脸核身结果查询
  1512. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  1513. $data = [
  1514. 'appId' => config('tencent_yun')['secret_id'],
  1515. 'version' => '1.0.0',
  1516. 'nonce' => $sign_data['nonce'],
  1517. 'orderNo' => $user_auth['out_trade_no'],
  1518. 'sign' => $sign
  1519. ];
  1520. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1521. if (!$rs) {
  1522. $this->error('您的网络开小差啦5~');
  1523. }
  1524. $rs = json_decode($rs, true);
  1525. if (!$rs || $rs['code'] != 0) {
  1526. $this->error($rs['msg']);
  1527. }
  1528. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  1529. $edit_data['status'] = 1;
  1530. $msg = '真人认证成功';
  1531. } else {
  1532. $edit_data['status'] = 2;
  1533. $edit_data['certify_id'] = '';
  1534. $edit_data['out_trade_no'] = '';
  1535. $msg = '真人认证失败';
  1536. }
  1537. $edit_data['updatetime'] = time();
  1538. //开启事务
  1539. Db::startTrans();
  1540. //修改认证信息
  1541. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1542. if (!$result) {
  1543. Db::rollback();
  1544. $this->error('查询认证结果失败2');
  1545. }
  1546. //修改用户信息
  1547. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', $edit_data['status']);
  1548. if (!$rs) {
  1549. Db::rollback();
  1550. $this->error('查询认证结果失败3');
  1551. }
  1552. if ($edit_data['status'] == 1) { //通过
  1553. //tag任务赠送金币
  1554. //真人认证奖励
  1555. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1556. if($task_rs === false){
  1557. Db::rollback();
  1558. $this->error('完成任务赠送奖励失败');
  1559. }
  1560. //系统消息
  1561. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1562. } else {
  1563. //系统消息
  1564. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1565. }
  1566. Db::commit();
  1567. $this->success($msg);
  1568. }
  1569. //客服
  1570. public function kefu() {
  1571. $type = input('type', 0, 'intval'); //客服位置: 0客服中心 1充值客服
  1572. if (!in_array($type, [0, 1])) {
  1573. $this->error('您的网络开小差啦~');
  1574. }
  1575. if ($type == 0) {
  1576. $user_id = config('site.customer_service_id') ? : 0; //指定客服id
  1577. } else {
  1578. $user_id = config('site.pay_customer_service_id') ? : 0; //指定客服id
  1579. }
  1580. $list = Db::name('user')->field('id')->where(['status' => 1, 'is_kefu' => 1, 'id' => $user_id])->select();
  1581. if (!$list) {
  1582. $this->success('success', $list);
  1583. }
  1584. foreach ($list as $k => &$v) {
  1585. $v['nickname'] = '客服' . ($k + 1);
  1586. $v['avatar'] = config('avatar_girl');
  1587. }
  1588. $this->success('success', $list);
  1589. }
  1590. //海报背景图
  1591. public function posterlist() {
  1592. $list = config('site.intro_images');
  1593. if (!$list) {
  1594. $this->success('success', (object)[]);
  1595. }
  1596. foreach ($list as &$v) {
  1597. $v = config('img_url') . $this->createposter(config('site.domain_cdnurl') . $v);
  1598. }
  1599. $this->success('success', $list);
  1600. }
  1601. //生成海报
  1602. public function createposter($image = '') {
  1603. // $image = input('image', '', 'trim');
  1604. // if (!$image) {
  1605. // $this->error('您的网络开小差啦~');
  1606. // }
  1607. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode, 'background' => $image]);
  1608. return $haibao;
  1609. // $this->success('success', $haibao);
  1610. }
  1611. //注册设置性别
  1612. public function setgender() {
  1613. $user_id = $this->auth->id;
  1614. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  1615. if (!in_array($gender, [1, 0])) {
  1616. $this->error('性别错误');
  1617. }
  1618. $edit_data['gender'] = $gender;
  1619. $edit_data['avatar'] = $gender == 1 ? config('avatar_boy') : config('avatar_girl'); //头像
  1620. $rs = Db::name('user')->where(['id' => $user_id, 'gender' => $this->auth->gender])->setField($edit_data);
  1621. if (!$rs) {
  1622. $this->error('您的网络开小差啦~');
  1623. }
  1624. //$data = $this->userInfo('return');
  1625. $data['gender'] = $edit_data['gender'];
  1626. $data['avatar'] = $edit_data['avatar'];
  1627. $this->success('success', $data);
  1628. }
  1629. //真人认证后修改头像前比对
  1630. public function realavatar_auit() {
  1631. if ($this->auth->real_status != 1) {
  1632. $this->error('尚未通过真人认证');
  1633. }
  1634. $avatar = input('avatar', '', 'trim'); //头像地址
  1635. if ($avatar === '') {
  1636. $this->error('参数缺失');
  1637. }
  1638. $avatar = one_domain_image($avatar);
  1639. $now_avatar = one_domain_image($this->auth->avatar);
  1640. if ($avatar == $now_avatar) {
  1641. $this->error('头像未改变');
  1642. }
  1643. //腾讯云人脸识别
  1644. $result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1645. $this->success('结果', $result);
  1646. }
  1647. //真人认证后修改头像
  1648. public function editrealavatar() {
  1649. if ($this->auth->real_status != 1) {
  1650. $this->error('尚未通过真人认证');
  1651. }
  1652. $avatar = input('avatar', '', 'trim'); //头像地址
  1653. if ($avatar === '') {
  1654. $this->error('参数缺失');
  1655. }
  1656. $avatar = one_domain_image($avatar);
  1657. $now_avatar = one_domain_image($this->auth->avatar);
  1658. if ($avatar == $now_avatar) {
  1659. $this->error('头像未改变');
  1660. }
  1661. //腾讯云人脸识别
  1662. $auit_result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1663. if ($auit_result != 1) {
  1664. $this->success('提示', ['code' => 2]);
  1665. }
  1666. $data['avatar'] = $avatar;
  1667. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1668. if (!$user_result) {
  1669. $this->error('修改失败');
  1670. }
  1671. $this->success('修改成功');
  1672. }
  1673. //真人认证后修改头像并取消真人认证
  1674. public function editrealavatarcancelauit() {
  1675. if ($this->auth->real_status != 1) {
  1676. $this->error('尚未通过真人认证');
  1677. }
  1678. $avatar = input('avatar', '', 'trim'); //头像地址
  1679. if ($avatar === '') {
  1680. $this->error('参数缺失');
  1681. }
  1682. $avatar = one_domain_image($avatar);
  1683. $now_avatar = one_domain_image($this->auth->avatar);
  1684. if ($avatar == $now_avatar) {
  1685. $this->error('头像未改变');
  1686. }
  1687. $data['avatar'] = $avatar;
  1688. $data['real_status'] = -1;
  1689. //开启事务
  1690. Db::startTrans();
  1691. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1692. if (!$user_result) {
  1693. Db::rollback();
  1694. $this->error('修改失败');
  1695. }
  1696. $user_auth_result = Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  1697. if (!$user_auth_result) {
  1698. Db::rollback();
  1699. $this->error('修改失败');
  1700. }
  1701. Db::commit();
  1702. $this->success('修改成功');
  1703. }
  1704. //腾讯云人脸识别
  1705. public function face_tencent($urla = '', $urlb = '') {
  1706. // require_once 'vendor/autoload.php';
  1707. try {
  1708. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1709. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1710. $config = config('tencent_im');
  1711. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1712. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1713. $httpProfile = new HttpProfile();
  1714. $httpProfile->setEndpoint("iai.tencentcloudapi.com");
  1715. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1716. $clientProfile = new ClientProfile();
  1717. $clientProfile->setHttpProfile($httpProfile);
  1718. // 实例化要请求产品的client对象,clientProfile是可选的
  1719. $client = new IaiClient($cred, "ap-beijing", $clientProfile);
  1720. // 实例化一个请求对象,每个接口都会对应一个request对象
  1721. $req = new CompareFaceRequest();
  1722. $params = array(
  1723. "UrlA" => $urla,
  1724. "UrlB" => $urlb,
  1725. "FaceModelVersion" => "3.0"
  1726. );
  1727. $req->fromJsonString(json_encode($params));
  1728. // 返回的resp是一个CompareFaceResponse的实例,与请求对象对应
  1729. $resp = $client->CompareFace($req);
  1730. // 输出json格式的字符串回包
  1731. // print_r($resp->toJsonString());
  1732. $result = json_decode($resp->toJsonString(), true);
  1733. //3.0版本误识率千分之一对应分数为40分,误识率万分之一对应分数为50分,误识率十万分之一对应分数为60分。 一般超过50分则可认定为同一人。
  1734. if (isset($result['Score']) && $result['Score'] >= 60) {
  1735. return 1; //通过
  1736. } else {
  1737. return 0;
  1738. }
  1739. }
  1740. catch(TencentCloudSDKException $e) {
  1741. // echo $e;
  1742. return 0;
  1743. }
  1744. }
  1745. //修改城市
  1746. public function editcity() {
  1747. $name = input('name', '', 'trim'); //城市名
  1748. if ($name === '') {
  1749. $this->error('参数缺失');
  1750. }
  1751. $hometown_cityid = Db::name('area')->where(['name' => $name])->value('id');
  1752. if (!$hometown_cityid) {
  1753. $this->success('修改成功');
  1754. }
  1755. Db::name('user')->where(['id' => $this->auth->id])->setField('hometown_cityid', $hometown_cityid);
  1756. $this->success('修改成功');
  1757. }
  1758. //搜索用户
  1759. public function searchuser() {
  1760. $keyword = input('keyword', '', 'trim'); //昵称或ID
  1761. if ($keyword === '') {
  1762. $this->error('请输入关键字');
  1763. }
  1764. $id = Db::name('user')->where(['nickname|username' => $keyword])->value('id');
  1765. $id = $id ? : 0;
  1766. $this->success('用户', $id);
  1767. }
  1768. //绑定支付宝账号信息
  1769. public function bindalipayaccount() {
  1770. $mt_user_bank = Db::name('user_bank');
  1771. $count = $mt_user_bank->where(['user_id' => $this->auth->id, 'type' => 1])->count();
  1772. if ($count) {
  1773. $this->error('您已绑定支付宝啦');
  1774. }
  1775. $auth_code = input('auth_code', '', 'trim');
  1776. if (!$auth_code) {
  1777. $this->error('授权码缺失');
  1778. }
  1779. //获取支付宝用户信息
  1780. $rs = getAlipayInfo($auth_code);
  1781. if ($rs['status'] == 0) {
  1782. $this->error($rs['info']);
  1783. }
  1784. $rs = $rs['data'];
  1785. /*
  1786. * Array(
  1787. [alipay_user_info_share_response] => Array
  1788. (
  1789. [code] => 10000
  1790. [msg] => Success
  1791. [avatar] => https://tfs.alipayobjects.com/images/partner/T1BQJsXhhbXXXXXXXX 头像
  1792. [city] => 临沂市 市名称。
  1793. [gender] => m 【注意】只有is_certified为T的时候才有意义,否则不保证准确性. 性别(F:女性;M:男性)。
  1794. [is_certified] => T 是否通过实名认证。T是通过 F是没有实名认证。
  1795. [is_student_certified] => F 是否是学生 T是 F否
  1796. [nick_name] => 风的追求 用户昵称
  1797. [province] => 山东省 省份名称
  1798. [user_id] => 2088902918001020 支付宝用户的userId
  1799. [user_status] => T 用户状态(Q/T/B/W)。 Q代表快速注册用户 T代表已认证用户 B代表被冻结账户 W代表已注册,未激活的账户
  1800. [user_type] => 2 用户类型(1/2) 1代表公司账户2代表个人账户
  1801. )
  1802. )
  1803. */
  1804. $data['banknumber'] = $rs['user_id']; //支付宝用户id
  1805. if (!$data['banknumber']) {
  1806. $this->error('获取用户信息失败');
  1807. }
  1808. $bank_number_count = $mt_user_bank->where(['banknumber' => $data['banknumber'], 'type' => 1])->count();
  1809. if ($bank_number_count) {
  1810. $this->error('该支付宝账号已经绑定用户, 请先解除绑定');
  1811. }
  1812. if (isset($rs['nick_name'])) {
  1813. $data['realname'] = $rs['nick_name']; //支付宝用户昵称
  1814. }
  1815. $data['user_id'] = $this->auth->id;
  1816. $data['createtime'] = time();
  1817. $data['type'] = 1;
  1818. $rt = $mt_user_bank->insertGetId($data);
  1819. if (!$rt) {
  1820. $this->error('绑定失败');
  1821. }
  1822. $this->success('绑定成功');
  1823. }
  1824. //添加打招呼内容,女性使用
  1825. public function addgreetcontent() {
  1826. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=相册
  1827. $title = input('title', '', 'trim'); //标题(type=1必传)
  1828. $content = input('content', '', 'trim'); //内容
  1829. $duration = input('duration', 0, 'intval'); //时长(type=1必传)
  1830. if (!in_array($type, [0, 1, 2])) {
  1831. $this->error('您的网络开小差了');
  1832. }
  1833. if ($content === '') {
  1834. $this->error('请设置打招呼内容');
  1835. }
  1836. if (iconv_strlen($content, 'utf-8') > 255) {
  1837. $this->error('打招呼内容最多255位');
  1838. }
  1839. if ($type == 1) {
  1840. if ($title === '') {
  1841. $this->error('请输入语音名称');
  1842. }
  1843. if (iconv_strlen($title, 'utf-8') > 50) {
  1844. $this->error('语音名称最多50字');
  1845. }
  1846. if (!$duration) {
  1847. $this->error('参数缺失');
  1848. }
  1849. }
  1850. $count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'type' => $type])->count('id');
  1851. if ($count > 10) {
  1852. $this->error('同一类型打招呼最多设置10条');
  1853. }
  1854. $data['user_id'] = $this->auth->id;
  1855. $data['type'] = $type;
  1856. $data['content'] = $content;
  1857. if ($type == 1) {
  1858. $data['title'] = $title;
  1859. $data['duration'] = $duration;
  1860. }
  1861. $data['createtime'] = time();
  1862. //查询是否有默认打招呼内容
  1863. $default_greet_count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'is_default' => 1])->count('id');
  1864. if (!$default_greet_count) {
  1865. $data['is_default'] = 1;
  1866. }
  1867. $rs = Db::name('user_greet_content')->insertGetId($data);
  1868. if (!$rs) {
  1869. $this->error('您的网络开小差了');
  1870. }
  1871. $this->success('设置成功');
  1872. }
  1873. //查询打招呼内容,女性使用
  1874. public function getgreetcontent() {
  1875. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=相册,3=所有招呼
  1876. if (!in_array($type, [0, 1, 2, 3])) {
  1877. $this->error('您的网络开小差了');
  1878. }
  1879. if ($type == 3) {
  1880. $list = Db::name('user_greet_content')->where(function ($query) {
  1881. $query->where('user_id', $this->auth->id)->where('type', 'neq', 1);
  1882. })->whereOr(function ($query) {
  1883. $query->where('user_id', $this->auth->id)->where('type', 1)->where('is_default', 1);
  1884. })->select();
  1885. } else {
  1886. $where['user_id'] = $this->auth->id;
  1887. $where['type'] = $type;
  1888. $list = Db::name('user_greet_content')->where($where)->select();
  1889. }
  1890. if ($list) {
  1891. foreach ($list as &$v) {
  1892. if ($v['type'] != 0) {
  1893. $v['content'] = one_domain_image($v['content']);
  1894. }
  1895. }
  1896. }
  1897. $this->success('打招呼内容', $list);
  1898. }
  1899. //设置默认打招呼内容,女性使用
  1900. public function setdefaultgreet() {
  1901. $id = input('id', 0, 'intval'); //打招呼id
  1902. if (!$id) {
  1903. $this->error('您的网络开小差了');
  1904. }
  1905. $count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'id' => $id])->count('id');
  1906. if (!$count) {
  1907. $this->error('您的网络开小差了');
  1908. }
  1909. //开启事务
  1910. Db::startTrans();
  1911. //清除之前默认
  1912. $rs = Db::name('user_greet_content')->where(['user_id' => $this->auth->id])->setField('is_default', 0);
  1913. if ($rs === false) {
  1914. Db::rollback();
  1915. $this->error('您的网络开小差了');
  1916. }
  1917. //设置
  1918. $rt = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'id' => $id])->setField('is_default', 1);
  1919. if ($rt === false) {
  1920. Db::rollback();
  1921. $this->error('您的网络开小差了');
  1922. }
  1923. Db::commit();
  1924. $this->success('设置成功');
  1925. }
  1926. //删除打招呼内容,女性使用
  1927. public function deldefaultgreet() {
  1928. $id = input('id', 0, 'intval'); //打招呼id
  1929. if (!$id) {
  1930. $this->error('您的网络开小差了');
  1931. }
  1932. $mt_user_greet_content = Db::name('user_greet_content');
  1933. $info = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $id])->find();
  1934. if (!$info) {
  1935. $this->error('您的网络开小差了');
  1936. }
  1937. //开启事务
  1938. Db::startTrans();
  1939. //删除
  1940. $rs = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $id])->delete();
  1941. if ($rs === false) {
  1942. Db::rollback();
  1943. $this->error('您的网络开小差了');
  1944. }
  1945. //若为默认打招呼内容则重新设置一条
  1946. if ($info['is_default'] == 1) {
  1947. $first_greet = $mt_user_greet_content->where(['user_id' => $this->auth->id])->find();
  1948. if ($first_greet) {
  1949. $rt = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $first_greet['id']])->setField('is_default', 1);
  1950. if ($rt === false) {
  1951. Db::rollback();
  1952. $this->error('您的网络开小差了');
  1953. }
  1954. }
  1955. }
  1956. Db::commit();
  1957. $this->success('删除成功');
  1958. }
  1959. }