User.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\ListCallTaskResponseBody\data;
  4. use app\common\controller\Api;
  5. use app\common\library\Ems;
  6. use app\common\library\Sms;
  7. use fast\Random;
  8. use think\Config;
  9. use think\Validate;
  10. use think\Db;
  11. use wxpay;
  12. /**
  13. * 会员接口
  14. */
  15. class User extends Api
  16. {
  17. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'registercheck', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getopenid', 'getagreement', 'wxlogin'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if (!Config::get('fastadmin.usercenter')) {
  23. $this->error(__('User center already closed'));
  24. }
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 会员登录
  35. *
  36. * @ApiMethod (POST)
  37. * @param string $account 账号
  38. * @param string $password 密码
  39. */
  40. public function login()
  41. {
  42. $account = $this->request->post('account');
  43. $password = $this->request->post('password');
  44. if (!$account || !$password) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. $ret = $this->auth->login($account, $password);
  48. if ($ret) {
  49. $data = ['userinfo' => $this->auth->getUserinfo()];
  50. $this->success(__('Logged in successful'), $data);
  51. } else {
  52. $this->error($this->auth->getError());
  53. }
  54. }
  55. /**
  56. * 手机验证码登录
  57. *
  58. * @ApiMethod (POST)
  59. * @param string $mobile 手机号
  60. * @param string $captcha 验证码
  61. */
  62. public function mobilelogin()
  63. {
  64. $mobile = $this->request->post('mobile');
  65. $captcha = $this->request->post('captcha');
  66. if (!$mobile || !$captcha) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. if (!Validate::regex($mobile, "^1\d{10}$")) {
  70. $this->error(__('Mobile is incorrect'));
  71. }
  72. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  73. $this->error(__('Captcha is incorrect'));
  74. }
  75. $user = \app\common\model\User::getByMobile($mobile);
  76. if (!$user) {
  77. $this->error('用户尚未注册');
  78. }
  79. if ($user['status'] != 1) {
  80. $this->error(__('Account is locked'));
  81. }
  82. // if ($user) {
  83. // if ($user->status != 'normal') {
  84. // $this->error(__('Account is locked'));
  85. // }
  86. // //如果已经有账号则直接登录
  87. // $ret = $this->auth->direct($user->id);
  88. // } else {
  89. // $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  90. // }
  91. $ret = $this->auth->direct($user->id);
  92. if ($ret) {
  93. Sms::flush($mobile, 'mobilelogin');
  94. $data = ['userinfo' => $this->auth->getUserinfo()];
  95. $this->success(__('Logged in successful'), $data);
  96. } else {
  97. $this->error($this->auth->getError());
  98. }
  99. }
  100. /**
  101. * 注册会员
  102. *
  103. * @ApiMethod (POST)
  104. * @param string $username 用户名
  105. * @param string $password 密码
  106. * @param string $email 邮箱
  107. * @param string $mobile 手机号
  108. * @param string $code 验证码
  109. */
  110. /*public function register()
  111. {
  112. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  113. $code = $this->request->post('code', '', 'trim'); //验证码
  114. $password = $this->request->post('password', '' , 'trim'); //密码
  115. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  116. $nickname = $this->request->post('nickname', '', 'trim'); //姓名
  117. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  118. $province = $this->request->post('province', '', 'trim'); //省
  119. $city = $this->request->post('city', '', 'trim'); //市
  120. $area = $this->request->post('area', '', 'trim'); //区
  121. $address = $this->request->post('address', '', 'trim'); //详细地址
  122. $recommender = $this->request->post('recommender', '', 'trim'); //推荐人姓名
  123. $recommender_mobile = $this->request->post('recommender_mobile', '', 'trim'); //推荐人手机号
  124. $zimage = $this->request->post('zimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证正面照
  125. $fimage = $this->request->post('fimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证反面照
  126. if (!Validate::regex($mobile, "^1\d{10}$")) {
  127. $this->error(__('Mobile is incorrect'));
  128. }
  129. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  130. if ($count) {
  131. $this->error('手机号已被注册');
  132. }
  133. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  134. $this->error(__('Captcha is incorrect'));
  135. }
  136. $ret = Sms::check($mobile, $code, 'register');
  137. if (!$ret) {
  138. $this->error(__('Captcha is incorrect'));
  139. }
  140. if (!$password) {
  141. $this->error('请输入新密码');
  142. }
  143. //验证新密码
  144. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  145. $this->error(__('Password must be 6 to 30 characters'));
  146. }
  147. if (!$repassword) {
  148. $this->error('请再次输入密码');
  149. }
  150. if ($repassword != $password) {
  151. $this->error('两次密码不一致');
  152. }
  153. if (!$nickname) {
  154. $this->error('请输入姓名');
  155. }
  156. if (iconv_strlen($nickname, 'utf-8') > 30) {
  157. $this->error('姓名最多30位');
  158. }
  159. if (!$idcard) {
  160. $this->error('请输入身份证号');
  161. }
  162. if (iconv_strlen($idcard, 'utf-8') != 18) {
  163. $this->error('身份证号错误');
  164. }
  165. if (!$province || !$city || !$area) {
  166. $this->error('请选择养殖场地址');
  167. }
  168. if (!$address) {
  169. $this->error('请输入详细地址');
  170. }
  171. if (iconv_strlen($address, 'utf-8') > 255) {
  172. $this->error('详细地址最多255位');
  173. }
  174. if (!$recommender) {
  175. $this->error('请输入推荐人姓名');
  176. }
  177. if (iconv_strlen($recommender, 'utf-8') > 30) {
  178. $this->error('推荐人姓名最多30位');
  179. }
  180. if (!$recommender_mobile || !is_mobile($recommender_mobile)) {
  181. $this->error('请输入正确推荐人手机号');
  182. }
  183. if (!$zimage || !$fimage) {
  184. $this->error('请上传身份证相关照片');
  185. }
  186. $ip = request()->ip();
  187. $time = time();
  188. $data = [
  189. 'nickname' => $nickname,
  190. 'province' => $province,
  191. 'city' => $city,
  192. 'area' => $area,
  193. 'address' => $address,
  194. 'createtime' => $time
  195. ];
  196. $params = array_merge($data, [
  197. 'mobile' => $mobile,
  198. 'password' => $password,
  199. 'avatar' => '/assets/img/avatar.png',
  200. 'salt' => Random::alnum(),
  201. 'jointime' => $time,
  202. 'joinip' => $ip,
  203. 'logintime' => $time,
  204. 'loginip' => $ip,
  205. 'prevtime' => $time,
  206. 'is_auth' => 1
  207. ]);
  208. $params['password'] = md5(md5($password) . $params['salt']);
  209. //开启事务
  210. Db::startTrans();
  211. $rs = Db::name('user')->insertGetId($params);
  212. if (!$rs) {
  213. Db::rollback();
  214. $this->error('注册失败');
  215. }
  216. $data['user_id'] = $rs;
  217. $data['idcard'] = $idcard;
  218. $data['zimage'] = $zimage;
  219. $data['fimage'] = $fimage;
  220. $data['recommender'] = $recommender;
  221. $data['recommender_mobile'] = $recommender_mobile;
  222. $rt = Db::name('user_auth')->insertGetId($data);
  223. if (!$rt) {
  224. Db::rollback();
  225. $this->error('注册失败');
  226. }
  227. Db::commit();
  228. $ret = $this->auth->login($mobile, $password);
  229. if ($ret) {
  230. $data = ['userinfo' => $this->auth->getUserinfo()];
  231. $this->success(__('Sign up successful'), $data);
  232. } else {
  233. $this->error($this->auth->getError());
  234. }
  235. }*/
  236. //注册第一步验证
  237. public function registercheck()
  238. {
  239. $mobile = $this->request->post('mobile'); //手机号
  240. $code = $this->request->post('code'); //验证码
  241. $password = $this->request->post('password'); //密码
  242. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  243. if (!Validate::regex($mobile, "^1\d{10}$")) {
  244. $this->error(__('Mobile is incorrect'));
  245. }
  246. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  247. if (!$count) {
  248. $this->error('手机号已被注册');
  249. }
  250. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  251. $this->error(__('Captcha is incorrect'));
  252. }
  253. $ret = Sms::check($mobile, $code, 'register');
  254. if (!$ret) {
  255. $this->error(__('Captcha is incorrect'));
  256. }
  257. if (!$password) {
  258. $this->error('请输入新密码');
  259. }
  260. //验证新密码
  261. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  262. $this->error(__('Password must be 6 to 30 characters'));
  263. }
  264. if (!$repassword) {
  265. $this->error('请再次输入密码');
  266. }
  267. if ($repassword != $password) {
  268. $this->error('两次密码不一致');
  269. }
  270. $this->success('验证通过');
  271. }
  272. /**
  273. * 退出登录
  274. * @ApiMethod (POST)
  275. */
  276. public function logout()
  277. {
  278. if (!$this->request->isPost()) {
  279. $this->error(__('Invalid parameters'));
  280. }
  281. $this->auth->logout();
  282. $this->success(__('Logout successful'));
  283. }
  284. //查询会员信息
  285. public function getinfo()
  286. {
  287. //检查今日是否登录赠送过成长值
  288. $this->checklogingrowth($this->auth->id);
  289. //检查会员等级
  290. $this->checkviplevel($this->auth->id);
  291. $user = Db::name('user')->find($this->auth->id);
  292. $data['nickname'] = $user['nickname']; //姓名
  293. $data['username'] = $user['username']; //UID
  294. $data['avatar'] = cdnurl($user['avatar']); //头像
  295. $data['mobile'] = $user['mobile']; //手机号
  296. $data['money'] = $user['money']; //余额
  297. $data['realname'] = $user['realname']; //真实姓名
  298. $data['gender'] = $user['gender']; //性别:1=男,2=女
  299. $data['birthday'] = date('Y-m-d', $user['birthday']); //生日
  300. $data['idcard'] = $user['idcard']; //身份证号
  301. $data['passport'] = $user['passport']; //护照号
  302. $data['emergencycontact'] = $user['emergencycontact']; //紧急联系人
  303. $data['contactmobile'] = $user['contactmobile']; //紧急联系方式
  304. $data['outdoorduration'] = $user['outdoorduration']; //户外时长
  305. $data['maxlevel'] = $user['maxlevel']; //实际有效会员ID
  306. $data['viplevel'] = Db::name('vip')->where(['id' => $user['maxlevel']])->value('level'); //会员等级
  307. $data['active_count'] = Db::name('active_order')->where(['user_id' => $this->auth->id, 'status' => 2])->count('id'); //完成活动数量
  308. $data['freenumber'] = $user['freenumber']; //会员免费参加活动次数
  309. $data['invite_no'] = $user['invite_no']; //邀请码
  310. $this->success('会员信息', $data);
  311. }
  312. /**
  313. * 修改会员个人信息
  314. *
  315. * @ApiMethod (POST)
  316. * @param string $avatar 头像地址
  317. * @param string $username 用户名
  318. * @param string $nickname 昵称
  319. * @param string $bio 个人简介
  320. */
  321. public function profile()
  322. {
  323. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //头像
  324. $nickname = $this->request->post('nickname', '', 'trim'); //昵称
  325. $realname = $this->request->post('realname', '', 'trim'); //真实姓名
  326. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  327. $passport = $this->request->post('passport', '', 'trim,strip_tags,htmlspecialchars'); //护照号
  328. $emergencycontact = $this->request->post('emergencycontact', '', 'trim'); //紧急联系人
  329. $contactmobile = $this->request->post('contactmobile', '', 'trim'); //紧急联系方式
  330. $outdoorduration = $this->request->post('outdoorduration', '', 'trim'); //户外时长
  331. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  332. $gender = $this->request->post('gender', 0, 'intval'); //性别
  333. $data = [];
  334. if ($avatar) {
  335. if (iconv_strlen($avatar, 'utf-8') > 255) {
  336. $this->error('图片超出范围');
  337. }
  338. $data['avatar'] = $avatar;
  339. }
  340. if ($nickname) {
  341. if (iconv_strlen($nickname, 'utf-8') > 30) {
  342. $this->error('昵称最多30字');
  343. }
  344. $data['nickname'] = $nickname;
  345. }
  346. if ($realname) {
  347. if(iconv_strlen($realname, 'utf-8') > 30) {
  348. $this->error('真实姓名最多30字');
  349. }
  350. $data['realname'] = $realname;
  351. }
  352. if ($idcard) {
  353. if (iconv_strlen($idcard, 'utf-8') != 18) {
  354. $this->error('身份证号错误');
  355. }
  356. $data['idcard'] = $idcard;
  357. }
  358. if ($passport) {
  359. if (iconv_strlen($passport, 'utf-8') > 30) {
  360. $this->error('护照号错误');
  361. }
  362. $data['passport'] = $passport;
  363. }
  364. if ($emergencycontact) {
  365. if(iconv_strlen($emergencycontact, 'utf-8') > 30) {
  366. $this->error('紧急联系人最多30字');
  367. }
  368. $data['emergencycontact'] = $emergencycontact;
  369. }
  370. if ($contactmobile) {
  371. if(!is_mobile($contactmobile)) {
  372. $this->error('请输入正确紧急联系方式');
  373. }
  374. $data['contactmobile'] = $contactmobile;
  375. }
  376. if ($outdoorduration) {
  377. if(iconv_strlen($outdoorduration, 'utf-8') > 50) {
  378. $this->error('户外时长最多50字');
  379. }
  380. $data['outdoorduration'] = $outdoorduration;
  381. }
  382. if ($birthday) {
  383. $data['birthday'] = $birthday;
  384. }
  385. if (in_array($gender, [1, 2])) {
  386. $data['gender'] = $gender;
  387. }
  388. // $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  389. // if ($username) {
  390. // $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  391. // if ($exists) {
  392. // $this->error(__('Username already exists'));
  393. // }
  394. // $user->username = $username;
  395. // }
  396. // if ($nickname) {
  397. // $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  398. // if ($exists) {
  399. // $this->error(__('Nickname already exists'));
  400. // }
  401. // $user->nickname = $nickname;
  402. // }
  403. // $user->avatar = $avatar;
  404. if (!$data) {
  405. $this->error('暂无修改内容');
  406. }
  407. //修改用户表
  408. $rt = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  409. if ($rt === false) {
  410. $this->error('修改失败');
  411. }
  412. $this->success('修改成功');
  413. }
  414. //修改头像
  415. public function changeavatar()
  416. {
  417. $avatar = input('avatar', '', 'trim'); //头像
  418. if (!$avatar) {
  419. $this->error('请上传头像');
  420. }
  421. if (iconv_strlen($avatar, 'utf-8') > 255) {
  422. $this->error('头像长度超出限制');
  423. }
  424. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('avatar', $avatar);
  425. if (!$rs) {
  426. $this->error('修改失败');
  427. }
  428. $this->success('修改成功');
  429. }
  430. /**
  431. * 修改邮箱
  432. *
  433. * @ApiMethod (POST)
  434. * @param string $email 邮箱
  435. * @param string $captcha 验证码
  436. */
  437. public function changeemail()
  438. {
  439. $user = $this->auth->getUser();
  440. $email = $this->request->post('email');
  441. $captcha = $this->request->post('captcha');
  442. if (!$email || !$captcha) {
  443. $this->error(__('Invalid parameters'));
  444. }
  445. if (!Validate::is($email, "email")) {
  446. $this->error(__('Email is incorrect'));
  447. }
  448. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  449. $this->error(__('Email already exists'));
  450. }
  451. $result = Ems::check($email, $captcha, 'changeemail');
  452. if (!$result) {
  453. $this->error(__('Captcha is incorrect'));
  454. }
  455. $verification = $user->verification;
  456. $verification->email = 1;
  457. $user->verification = $verification;
  458. $user->email = $email;
  459. $user->save();
  460. Ems::flush($email, 'changeemail');
  461. $this->success();
  462. }
  463. /**
  464. * 修改手机号
  465. *
  466. * @ApiMethod (POST)
  467. * @param string $mobile 手机号
  468. * @param string $captcha 验证码
  469. */
  470. public function changemobile()
  471. {
  472. $user = $this->auth->getUser();
  473. $code = $this->request->post('code');
  474. $mobile = $this->request->post('mobile');
  475. $captcha = $this->request->post('captcha');
  476. if (!$code || !$mobile || !$captcha) {
  477. $this->error(__('Invalid parameters'));
  478. }
  479. if ($mobile == $user->mobile) {
  480. $this->error('手机号没有改变');
  481. }
  482. $result = Sms::check($user->mobile, $code, 'changeyuanmobile');
  483. if (!$result) {
  484. $this->error(__('Captcha is incorrect'));
  485. }
  486. if (!Validate::regex($mobile, "^1\d{10}$")) {
  487. $this->error(__('Mobile is incorrect'));
  488. }
  489. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  490. $this->error(__('手机号已存在'));
  491. }
  492. $result = Sms::check($mobile, $captcha, 'changemobile');
  493. if (!$result) {
  494. $this->error(__('Captcha is incorrect'));
  495. }
  496. $verification = $user->verification;
  497. $verification->mobile = 1;
  498. $user->verification = $verification;
  499. $user->mobile = $mobile;
  500. $user->save();
  501. Sms::flush($user->mobile, 'changemobile');
  502. Sms::flush($mobile, 'changemobile');
  503. $this->success('修改成功');
  504. }
  505. /**
  506. * 第三方登录
  507. *
  508. * @ApiMethod (POST)
  509. * @param string $platform 平台名称
  510. * @param string $code Code码
  511. */
  512. public function third()
  513. {
  514. $url = url('user/index');
  515. $platform = $this->request->post("platform");
  516. $code = $this->request->post("code");
  517. $config = get_addon_config('third');
  518. if (!$config || !isset($config[$platform])) {
  519. $this->error(__('Invalid parameters'));
  520. }
  521. $app = new \addons\third\library\Application($config);
  522. //通过code换access_token和绑定会员
  523. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  524. if ($result) {
  525. $loginret = \addons\third\library\Service::connect($platform, $result);
  526. if ($loginret) {
  527. $data = [
  528. 'userinfo' => $this->auth->getUserinfo(),
  529. 'thirdinfo' => $result
  530. ];
  531. $this->success(__('Logged in successful'), $data);
  532. }
  533. }
  534. $this->error(__('Operation failed'), $url);
  535. }
  536. /**
  537. * 重置密码
  538. *
  539. * @ApiMethod (POST)
  540. * @param string $mobile 手机号
  541. * @param string $newpassword 新密码
  542. * @param string $captcha 验证码
  543. */
  544. public function resetpwd()
  545. {
  546. $type = 'mobile';//$this->request->post("type");
  547. $mobile = $this->request->post("mobile");
  548. $email = $this->request->post("email");
  549. $newpassword = $this->request->post("newpassword");
  550. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  551. $captcha = $this->request->post("captcha");
  552. if (!$newpassword || !$captcha || !$repassword) {
  553. $this->error(__('Invalid parameters'));
  554. }
  555. //验证Token
  556. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  557. $this->error(__('Password must be 6 to 30 characters'));
  558. }
  559. if ($repassword != $newpassword) {
  560. $this->error('两次密码不一致');
  561. }
  562. if ($type == 'mobile') {
  563. if (!Validate::regex($mobile, "^1\d{10}$")) {
  564. $this->error(__('Mobile is incorrect'));
  565. }
  566. $user = \app\common\model\User::getByMobile($mobile);
  567. if (!$user) {
  568. $this->error(__('User not found'));
  569. }
  570. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  571. if (!$ret) {
  572. $this->error(__('Captcha is incorrect'));
  573. }
  574. Sms::flush($mobile, 'resetpwd');
  575. } else {
  576. if (!Validate::is($email, "email")) {
  577. $this->error(__('Email is incorrect'));
  578. }
  579. $user = \app\common\model\User::getByEmail($email);
  580. if (!$user) {
  581. $this->error(__('User not found'));
  582. }
  583. $ret = Ems::check($email, $captcha, 'resetpwd');
  584. if (!$ret) {
  585. $this->error(__('Captcha is incorrect'));
  586. }
  587. Ems::flush($email, 'resetpwd');
  588. }
  589. //模拟一次登录
  590. $this->auth->direct($user->id);
  591. $ret = $this->auth->changepwd($newpassword, '', true);
  592. if ($ret) {
  593. $this->success(__('Reset password successful'));
  594. } else {
  595. $this->error($this->auth->getError());
  596. }
  597. }
  598. /**
  599. * 修改密码
  600. *
  601. * @ApiMethod (POST)
  602. * @param string $captcha 验证码
  603. * @param string $newpassword 新密码
  604. * @param string $repassword 确认密码
  605. */
  606. public function editpwd()
  607. {
  608. $captcha = $this->request->post("captcha", '', 'trim'); //验证码
  609. $newpassword = $this->request->post("newpassword", '' , 'trim'); //新密码
  610. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  611. if (iconv_strlen($captcha, 'utf-8') != config('alisms.length')) {
  612. $this->error(__('Captcha is incorrect'));
  613. }
  614. if (!$newpassword) {
  615. $this->error('请输入新密码');
  616. }
  617. //验证新密码
  618. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  619. $this->error(__('Password must be 6 to 30 characters'));
  620. }
  621. if (!$repassword) {
  622. $this->error('请再次输入密码');
  623. }
  624. if ($repassword != $newpassword) {
  625. $this->error('两次密码不一致');
  626. }
  627. // $user = \app\common\model\User::getById($this->auth->id);
  628. // if (!$user) {
  629. // $this->error(__('User not found'));
  630. // }
  631. $ret = Sms::check($this->auth->mobile, $captcha, 'changepwd');
  632. if (!$ret) {
  633. $this->error(__('Captcha is incorrect'));
  634. }
  635. //模拟一次登录
  636. $this->auth->direct($this->auth->id);
  637. $ret = $this->auth->changepwd($newpassword, '', true);
  638. if ($ret) {
  639. Sms::flush($this->auth->mobile, 'changepwd');
  640. $this->success(__('Reset password successful'));
  641. } else {
  642. $this->error($this->auth->getError());
  643. }
  644. }
  645. //查询经营信息
  646. public function get_businessinfo()
  647. {
  648. $data = Db::name('user_business_info')->field('manage, mobile, province, city, area, distribution_channel')
  649. ->where(['user_id' => $this->auth->id])->find();
  650. $this->success('经营信息', $data);
  651. }
  652. //修改经营信息
  653. public function editbusinessinfo()
  654. {
  655. $manage = $this->request->post('manage', '', 'trim'); //采购负责人
  656. $mobile = $this->request->post('mobile', '', 'trim'); //联系方式
  657. $province = $this->request->post('province', '', 'trim'); //省
  658. $city = $this->request->post('city', '', 'trim'); //市
  659. $area = $this->request->post('area', '', 'trim'); //区
  660. $distribution_channel = $this->request->post('distribution_channel', '', 'trim'); //销售渠道
  661. $data = [];
  662. if ($manage) {
  663. if (iconv_strlen($manage, 'utf-8') > 50) {
  664. $this->error('采购负责人最多50字');
  665. }
  666. $data['manage'] = $manage;
  667. }
  668. if ($mobile) {
  669. if (!Validate::regex($mobile, "^1\d{10}$")) {
  670. $this->error(__('Mobile is incorrect'));
  671. }
  672. $data['mobile'] = $mobile;
  673. }
  674. if ($province && $city && $area) {
  675. $data['province'] = $province;
  676. $data['city'] = $city;
  677. $data['area'] = $area;
  678. }
  679. if ($distribution_channel) {
  680. if (iconv_strlen($distribution_channel, 'utf-8') > 255) {
  681. $this->error('销售渠道说明最多50字');
  682. }
  683. $data['distribution_channel'] = $distribution_channel;
  684. }
  685. if (!$data) {
  686. $this->error('暂无修改内容');
  687. }
  688. $count = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->count();
  689. if ($count) {
  690. $data['updatetime'] = time();
  691. $rs = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->setField($data);
  692. } else {
  693. $data['user_id'] = $this->auth->id;
  694. $data['createtime'] = time();
  695. $rs = Db::name('user_business_info')->insertGetId($data);
  696. }
  697. if (!$rs) {
  698. $this->error('修改失败');
  699. }
  700. $this->success('修改成功');
  701. }
  702. //查询猪车信息
  703. public function getpigcar()
  704. {
  705. $data = Db::name('user_pig_car')->where(['user_id' => $this->auth->id])->select();
  706. $data = list_domain_image($data, ['images']);
  707. $this->success('查询猪车信息', $data);
  708. }
  709. /**
  710. * 添加猪车信息
  711. *
  712. *$data = [
  713. [
  714. 'carnumber' => '123',
  715. 'carframenumber' => '344',
  716. 'images' => '123.jpg,234.jpg,345.jpg'
  717. ],
  718. [
  719. 'carnumber' => '123',
  720. 'carframenumber' => '344',
  721. 'images' => '123.jpg,234.jpg,345.jpg'
  722. ],
  723. [
  724. 'carnumber' => '123',
  725. 'carframenumber' => '344',
  726. 'images' => '123.jpg,234.jpg,345.jpg'
  727. ]
  728. ];
  729. *
  730. */
  731. public function addpigcar()
  732. {
  733. $pig_car = input('pig_car', '', 'trim'); //猪车信息json串: carnumber车牌号 carframenumber车架号 images车辆照片
  734. if (!$pig_car) {
  735. $this->error('请添加猪车信息');
  736. }
  737. $pig_car = json_decode($pig_car, true);
  738. if (!$pig_car) {
  739. $this->error('请添加猪车信息');
  740. }
  741. if (count($pig_car) > 9) {
  742. $this->error('一次最多添加9条记录');
  743. }
  744. $_data = [];
  745. foreach ($pig_car as &$v) {
  746. $data = [];
  747. if (!$v['carnumber']) {
  748. $this->error('请输入车牌号');
  749. break;
  750. }
  751. if (iconv_strlen($v['carnumber'], 'utf-8') > 50) {
  752. $this->error('车牌号最多50字');
  753. break;
  754. }
  755. if (!$v['carframenumber']) {
  756. $this->error('请输入车架号');
  757. break;
  758. }
  759. if (iconv_strlen($v['carframenumber'], 'utf-8') > 50) {
  760. $this->error('车架号最多50字');
  761. break;
  762. }
  763. if ($v['images']) {
  764. $image_arr = explode(',', $v['images']);
  765. if (count($image_arr) > 9) {
  766. $this->error('每辆车照片最多9张');
  767. break;
  768. }
  769. $data['images'] = $v['images'];
  770. }
  771. $data['user_id'] = $this->auth->id;
  772. $data['carnumber'] = $v['carnumber'];
  773. $data['carframenumber'] = $v['carframenumber'];
  774. $data['createtime'] = time();
  775. array_push($_data, $data);
  776. }
  777. $rs = Db::name('user_pig_car')->insertAll($_data);
  778. if (!$rs) {
  779. $this->error('添加失败');
  780. }
  781. $this->success('添加成功');
  782. }
  783. //删除猪车信息
  784. public function delpigcar()
  785. {
  786. $id = input('id', 0, 'intval'); //猪车id
  787. if (!$id) {
  788. $this->error('请选择要删除的猪车');
  789. }
  790. $info = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  791. if (!$info) {
  792. $this->error('这不是您的车');
  793. }
  794. $rs = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->delete();
  795. if (!$rs) {
  796. $this->error('删除失败');
  797. }
  798. $this->success('删除成功');
  799. }
  800. //平台介绍
  801. public function getabout()
  802. {
  803. $info = Db::name('platform_info')->field('title, content')->where(['type' => 1])->find();
  804. $this->success('平台介绍', $info);
  805. }
  806. //常见问题
  807. public function problem()
  808. {
  809. $list = Db::name('problem')->field('id, title, content')
  810. ->page($this->page, config('paginate.list_rows'))->order('weigh', 'desc')->select();
  811. $this->success('常见问题', $list);
  812. }
  813. //投诉举报类型
  814. public function complainttype()
  815. {
  816. $list = Db::name('complaint_type')->field('id, name')->where(['pid' => 0])->order('weigh', 'desc')->select();
  817. $list = $this->getchildtype($list);
  818. $this->success('常见问题', $list);
  819. }
  820. //无限级投诉举报类型
  821. public function getchildtype($list = [])
  822. {
  823. $complaint_type = Db::name('complaint_type');
  824. foreach ($list as &$v) {
  825. $child = $complaint_type->field('id, name')->where(['pid' => $v['id']])->order('weigh', 'desc')->select();
  826. if ($child) {
  827. $child = $this->getchildtype($child);
  828. }
  829. $v['child'] = $child;
  830. }
  831. return $list;
  832. }
  833. //投诉举报
  834. public function complaint()
  835. {
  836. $complaint_type = input('complaint_type', '', 'trim'); //投诉类型
  837. $order_sn = input('order_sn', '', 'trim'); //订单号
  838. $be_complaint_user = input('be_complaint_user', '', 'trim'); //被投诉人/单位
  839. $complaint_user = input('complaint_user', '', 'trim'); //投诉人
  840. $mobile = input('mobile', '', 'trim'); //联系电话
  841. $code = input('code', '', 'trim'); //验证码
  842. $content = input('content', '', 'trim'); //情况说明
  843. $images = input('images', '', 'trim'); //附件材料
  844. $data = [];
  845. if (!$complaint_type) {
  846. $this->error('请选择投诉类型');
  847. }
  848. if ($order_sn) {
  849. if (iconv_strlen($order_sn, 'utf-8') > 255) {
  850. $this->error('订单号最多255位');
  851. }
  852. //查询订单
  853. $count = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'order_sn' => $order_sn, 'status' => 1])->count();
  854. if (!$count) {
  855. $this->error('请输入正确中标订单号');
  856. }
  857. }
  858. if (!$be_complaint_user) {
  859. $this->error('请输入被投诉人/单位');
  860. }
  861. if (iconv_strlen($be_complaint_user, 'utf-8') > 50) {
  862. $this->error('被投诉人/单位最多50字');
  863. }
  864. if (!$complaint_user) {
  865. $this->error('请输入投诉人');
  866. }
  867. if (iconv_strlen($complaint_user, 'utf-8') > 50) {
  868. $this->error('投诉人最多50字');
  869. }
  870. if (!is_mobile($mobile)) {
  871. $this->error('请输入正确联系电话');
  872. }
  873. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  874. $this->error(__('Captcha is incorrect'));
  875. }
  876. $ret = Sms::check($mobile, $code, 'complaint');
  877. if (!$ret) {
  878. $this->error(__('Captcha is incorrect'));
  879. }
  880. if (!$content) {
  881. $this->error('请输入情况说明');
  882. }
  883. if (iconv_strlen($content, 'utf-8') > 3000) {
  884. $this->error('情况说明最多3000字');
  885. }
  886. if ($images) {
  887. $image_arr = explode(',', $images);
  888. if (count($image_arr) > 9) {
  889. $this->error('附件照片最多9张');
  890. }
  891. $data['images'] = $images;
  892. }
  893. $data['user_id'] = $this->auth->id;
  894. $data['complaint_type'] = $complaint_type;
  895. $data['order_sn'] = $order_sn;
  896. $data['be_complaint_user'] = $be_complaint_user;
  897. $data['complaint_user'] = $complaint_user;
  898. $data['mobile'] = $mobile;
  899. $data['content'] = $content;
  900. $data['images'] = $images;
  901. $data['createtime'] = time();
  902. $rs = Db::name('complaint')->insertGetId($data);
  903. if (!$rs) {
  904. $this->error('投诉失败');
  905. }
  906. Sms::flush($this->auth->mobile, 'complaint');
  907. $this->success('投诉成功');
  908. }
  909. //评价列表
  910. public function getmarkcomment()
  911. {
  912. $list = Db::name('mark_bid_comment')->where(['user_id' => $this->auth->id])
  913. ->page($this->page, config('paginate.list_rows'))->order('createtime', 'desc')->select();
  914. $list = list_domain_image($list, ['images']);
  915. $mark = Db::name('mark');
  916. $mark_bid = Db::name('mark_bid');
  917. foreach ($list as &$v) {
  918. $v['mark'] = $mark->find($v['mark_id']);
  919. $v['mark'] = info_domain_image($v['mark'], ['image', 'video', 'defective_images', 'road_images', 'pig_out_images']);
  920. $v['order_sn'] = $mark_bid->where(['id' => $v['mark_bid_id']])->value('order_sn');
  921. }
  922. $this->success('评论列表', $list);
  923. }
  924. //评价
  925. public function markcomment()
  926. {
  927. $mark_id = input('mark_id', 0, 'intval'); //招标id
  928. $content = input('content', '', 'trim'); //情况说明
  929. $images = input('images', '', 'trim'); //附件材料
  930. $data = [];
  931. if (!$mark_id) {
  932. $this->error('请选择要评价的数据');
  933. }
  934. $info = Db::name('mark')->find($mark_id);
  935. if (!$info) {
  936. $this->error('招标信息不存在');
  937. }
  938. $bid_id = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'mark_id' => $mark_id, 'status' => 1])->value('id');
  939. if (!$bid_id) {
  940. $this->error('您未中标,暂不能评价');
  941. }
  942. if (!$content) {
  943. $this->error('请输入评价内容');
  944. }
  945. if (iconv_strlen($content, 'utf-8') > 3000) {
  946. $this->error('评价内容最多3000字');
  947. }
  948. if ($images) {
  949. $image_arr = explode(',', $images);
  950. if (count($image_arr) > 9) {
  951. $this->error('照片最多9张');
  952. }
  953. $data['images'] = $images;
  954. }
  955. $data['user_id'] = $this->auth->id;
  956. $data['mark_id'] = $mark_id;
  957. $data['mark_bid_id'] = $bid_id;
  958. $data['content'] = $content;
  959. $data['images'] = $images;
  960. $data['createtime'] = time();
  961. $rs = Db::name('mark_bid_comment')->insertGetId($data);
  962. if (!$rs) {
  963. $this->error('评价失败');
  964. }
  965. $this->success('评价成功');
  966. }
  967. //个人账单列表
  968. public function getcompanymoney()
  969. {
  970. $list = Db::name('company')->field('id, name')->page($this->page, config('paginate.list_rows'))->select();
  971. $company_money_log = Db::name('company_money_log');
  972. foreach ($list as &$v) {
  973. $log = $company_money_log->where(['company_id' => $v['id'], 'user_id' => $this->auth->id])->order('id', 'desc')->find();
  974. if ($log) {
  975. $v['money'] = $log['after'];
  976. $v['can_money'] = $log['after'];
  977. } else {
  978. $v['money'] = '0';
  979. $v['can_money'] = '0';
  980. }
  981. }
  982. $this->success('个人账单列表', $list);
  983. }
  984. //个人账单明细
  985. public function getcompanymoneydetail()
  986. {
  987. $company_id = input('company_id', 0, 'intval'); //公司id
  988. $start_time = input('start_time', 0, 'strtotime'); //开始时间
  989. $end_time = input('end_time', 0, 'strtotime'); //结束时间
  990. if (!$company_id) {
  991. $this->error('参数缺失');
  992. }
  993. $where['company_id'] = $company_id;
  994. $where['user_id'] = $this->auth->id;
  995. if ($start_time && $end_time) {
  996. $where['createtime'] = ['between', [$start_time, $end_time]];
  997. }
  998. $list = Db::name('company_money_log')->field('id, money, memo, createtime')
  999. ->where($where)->page($this->page, config('paginate.list_rows'))->order('id', 'desc')->select();
  1000. foreach ($list as &$v) {
  1001. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  1002. }
  1003. $data['income'] = Db::name('company_money_log')->where($where)->where(['money' => ['gt', 0]])->sum('money');
  1004. $data['expend'] = Db::name('company_money_log')->where($where)->where(['money' => ['lt', 0]])->sum('money');
  1005. $data['list'] = $list;
  1006. $this->success('个人账单明细', $data);
  1007. }
  1008. //关于我们/免责协议/用户协议/隐私政策/段位介绍
  1009. public function getagreement()
  1010. {
  1011. $type = input('type', 0, 'intval');
  1012. if (!in_array($type, [1, 2, 3, 4, 5])) {
  1013. $this->error('参数错误');
  1014. }
  1015. $info = Db::name('platform_info')->field('title, content')->where(['type' => $type])->find();
  1016. $this->success('协议', $info);
  1017. }
  1018. //获取openid
  1019. public function getopenid() {
  1020. //code
  1021. $code = $this->request->param('code', '', 'trim');// code值
  1022. if (!$code) {
  1023. $this->error(__('Invalid parameters'));
  1024. }
  1025. $config = config('wxchatpay');
  1026. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&js_code='.$code.'&grant_type=authorization_code';
  1027. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  1028. $openidInfo = json_decode($openidInfo,true);
  1029. if(!isset($openidInfo['openid'])) {
  1030. $this->error('用户openid获取失败', $openidInfo);
  1031. }
  1032. $this->success('success', $openidInfo);
  1033. }
  1034. //微信登录
  1035. public function wxlogin() {
  1036. $openid = input('openid', '', 'trim');
  1037. if (!$openid) {
  1038. $this->error('参数缺失');
  1039. }
  1040. $user = \app\common\model\User::getByOpenid($openid);
  1041. if (!$user) {
  1042. $this->success('用户尚未注册', ['code' => 5]);
  1043. }
  1044. if ($user['status'] != 1) {
  1045. $this->error(__('Account is locked'));
  1046. }
  1047. $ret = $this->auth->direct($user->id);
  1048. if ($ret) {
  1049. $data = ['userinfo' => $this->auth->getUserinfo()];
  1050. $this->success(__('Logged in successful'), $data);
  1051. } else {
  1052. $this->error($this->auth->getError());
  1053. }
  1054. }
  1055. //注册
  1056. public function register()
  1057. {
  1058. $realname = input('realname', '', 'trim'); //真实姓名
  1059. $height = input('height', 170, 'intval'); //身高
  1060. $weight = input('weight', 60, 'intval'); //体重
  1061. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  1062. $province_id = input('province_id', 0, 'intval'); //省id
  1063. $city_id = input('city_id', 0, 'intval'); //市id
  1064. $area_id = input('area_id', 0, 'intval'); //区id
  1065. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  1066. // $code = $this->request->post('code', '', 'trim'); //验证码
  1067. $idcard = input('idcard', '', 'trim'); //身份证号
  1068. $excel_position = input('excel_position', '', 'trim'); //擅长位置
  1069. // $gender = $this->request->post('gender', 0, 'intval'); //性别:1=男,2=女
  1070. // $invite_no = $this->request->post('invite_no', '', 'trim'); //邀请码
  1071. $openid = $this->request->post('openid', '', 'trim'); //微信openid
  1072. // $nickname = $this->request->post('nickname', '', 'trim'); //微信昵称
  1073. // $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //微信头像
  1074. $openidcount = Db::name('user')->where(['openid' => $openid])->count('id');
  1075. if ($openidcount) {
  1076. $this->error('微信已经注册,请直接登录');
  1077. }
  1078. if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
  1079. $this->error('真实姓名1-30位');
  1080. }
  1081. if ($height <= 0) {
  1082. $this->error('请选择正确身高');
  1083. }
  1084. if ($weight <= 0) {
  1085. $this->error('请选择正确体重');
  1086. }
  1087. if (!$birthday || $birthday >= time()) {
  1088. $this->error('请选择正确生日');
  1089. }
  1090. if (!$province_id || !$city_id || !$area_id) {
  1091. $this->error('请选择地区');
  1092. }
  1093. if (!Validate::regex($mobile, "^1\d{10}$")) {
  1094. $this->error(__('Mobile is incorrect'));
  1095. }
  1096. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  1097. if ($count) {
  1098. $this->error('手机号已被注册');
  1099. }
  1100. // if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  1101. // $this->error(__('Captcha is incorrect'));
  1102. // }
  1103. // $ret = Sms::check($mobile, $code, 'register');
  1104. // if (!$ret) {
  1105. // $this->error(__('Captcha is incorrect'));
  1106. // }
  1107. // if (!in_array($gender, [1, 2])) {
  1108. // $this->error('请选择性别');
  1109. // }
  1110. // if ($invite_no) {
  1111. // $invite_info = Db::name('user')->where(['invite_no' => $invite_no])->find();
  1112. // if (!$invite_info) {
  1113. // $this->error('邀请码不存在');
  1114. // }
  1115. // }
  1116. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1117. $this->error('请输入正确身份证号');
  1118. }
  1119. if ($excel_position === '' || iconv_strlen($excel_position, 'utf-8') > 30) {
  1120. $this->error('请选择擅长位置');
  1121. }
  1122. // if (!$nickname || !$avatar) {
  1123. // $this->error('参数缺失');
  1124. // }
  1125. // if (iconv_strlen($nickname, 'utf-8') > 30 || iconv_strlen($avatar, 'utf-8') > 255) {
  1126. // $this->error('参数错误');
  1127. // }
  1128. $ip = request()->ip();
  1129. $time = time();
  1130. $data = [
  1131. // 'nickname' => $nickname,
  1132. 'salt' => Random::alnum(),
  1133. 'mobile' => $mobile,
  1134. 'avatar' => config('logo'),//$avatar,
  1135. 'joinip' => $ip,
  1136. 'jointime' => $time,
  1137. 'logintime' => $time,
  1138. 'loginip' => $ip,
  1139. 'prevtime' => $time,
  1140. 'createtime' => $time,
  1141. // 'gender' => $gender,
  1142. 'birthday' => $birthday,
  1143. 'openid' => $openid,
  1144. 'invite_no' => $this->myinvite(),
  1145. 'pre_user_id' => isset($invite_info) ? $invite_info['id'] : 0,
  1146. 'invite_time' => $time,
  1147. 'realname' => $realname,
  1148. 'height' => $height,
  1149. 'weight' => $weight,
  1150. 'province_id' => $province_id,
  1151. 'city_id' => $city_id,
  1152. 'area_id' => $area_id,
  1153. 'idcard' => $idcard,
  1154. 'excel_position' => $excel_position,
  1155. ];
  1156. //开启事务
  1157. Db::startTrans();
  1158. $rs = Db::name('user')->insertGetId($data);
  1159. if (!$rs) {
  1160. Db::rollback();
  1161. $this->error('注册失败');
  1162. }
  1163. //生成uid
  1164. $username = $this->myuid($rs);
  1165. $rt = Db::name('user')->where(['id' => $rs])->setField(['username' => $username, 'nickname' => '球员' . $username]);
  1166. if (!$rt) {
  1167. Db::rollback();
  1168. $this->error('注册失败');
  1169. }
  1170. Db::commit();
  1171. $ret = $this->auth->direct($rs);
  1172. if ($ret) {
  1173. $data = ['userinfo' => $this->auth->getUserinfo()];
  1174. $this->success(__('Sign up successful'), $data);
  1175. } else {
  1176. $this->error($this->auth->getError());
  1177. }
  1178. }
  1179. //生成邀请码
  1180. public function myinvite() {
  1181. $invite = Random::alnum(7);
  1182. $count = Db::name('user')->where(['invite_no' => $invite])->count('id');
  1183. if ($count) {
  1184. $this->myinvite();
  1185. }
  1186. return $invite;
  1187. }
  1188. //生成uid
  1189. public function myuid($id = 0) {
  1190. if (strlen($id) < 8) {
  1191. $username_len = 8 - strlen($id);
  1192. $username = $id . Random::numeric($username_len);
  1193. } else {
  1194. $username = $id;
  1195. }
  1196. $count = Db::name('user')->where(['username' => $username])->count('id');
  1197. if ($count) {
  1198. $this->myuid($id);
  1199. }
  1200. return $username;
  1201. }
  1202. //查询是否有未读消息
  1203. public function getreadsysmsg() {
  1204. $count = Db::name('sys_msg')->where(['user_id' => $this->auth->id, 'is_read' => 0])->count('id');
  1205. $this->success('查询是否有未读消息', $count);
  1206. }
  1207. //消息列表
  1208. public function sysmsg() {
  1209. $list = Db::name('sys_msg')->field('id, title, content, createtime')->where(['user_id' => $this->auth->id])
  1210. ->page($this->page, $this->pagenum)->order('id desc')->select();
  1211. foreach ($list as &$v) {
  1212. $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
  1213. }
  1214. //更改消息状态
  1215. Db::name('sys_msg')->where(['user_id' => $this->auth->id, 'is_read' => 0])->setField('is_read', 1);
  1216. $this->success('消息列表', $list);
  1217. }
  1218. //查询当前会员等级信息
  1219. public function uservip() {
  1220. //更新会员等级
  1221. $this->checkviplevel($this->auth->id);
  1222. //用户信息
  1223. $user = Db::name('user')->find($this->auth->id);
  1224. //会员信息
  1225. $vip_info = Db::name('vip')->find($user['maxlevel']);
  1226. //下一级所需成长值
  1227. $next_vip = Db::name('vip')->where(['id' => ['gt', $user['maxlevel']]])->find();
  1228. if (!$next_vip || $next_vip['id'] == 5) {
  1229. //顶部信息
  1230. $vip_info['vip_desc'] = '已是最高会员';
  1231. //成长值百分比0-100
  1232. $vip_info['growthvalue_percentage'] = 100;
  1233. } else {
  1234. //顶部信息
  1235. $need_growthvalue = $next_vip['growthvalue'] - $user['growthvalue'];
  1236. $vip_info['vip_desc'] = $vip_info['title'] . '还需' . $need_growthvalue . '成长值成为' . $next_vip['title'];
  1237. //成长值百分比0-100
  1238. $vip_info['growthvalue_percentage'] = ceil($user['growthvalue'] / $next_vip['growthvalue'] * 100);
  1239. }
  1240. //体验会员到期时间
  1241. $vip_info['experiencetime'] = $user['experiencetime'] > time() ? date('Y-m-d', $user['experiencetime']) : '';
  1242. //判断是不是生日 1是 0否
  1243. $vip_info['is_birth'] = date('md', time()) == date('md', strtotime($this->auth->birthday)) ? 1: 0;
  1244. $this->success('查询当前会员等级信息', $vip_info);
  1245. }
  1246. //查询所有会员
  1247. public function vip() {
  1248. //更新会员等级
  1249. $this->checkviplevel($this->auth->id);
  1250. //用户信息
  1251. $user = Db::name('user')->find($this->auth->id);
  1252. //会员信息
  1253. $vip_info = Db::name('vip')->find($user['growthlevel']);
  1254. //下一级所需成长值
  1255. $next_vip = Db::name('vip')->where(['id' => ['gt', $user['growthlevel']]])->find();
  1256. if (!$next_vip || $next_vip['id'] == 5) {
  1257. //顶部信息
  1258. $vip_info['vip_desc'] = '已是最高会员';
  1259. //成长值百分比0-100
  1260. $vip_info['growthvalue_percentage'] = 100;
  1261. } else {
  1262. //顶部信息
  1263. $need_growthvalue = $next_vip['growthvalue'] - $user['growthvalue'];
  1264. $vip_info['vip_desc'] = $vip_info['title'] . '还需' . $need_growthvalue . '成长值成为' . $next_vip['title'];
  1265. //成长值百分比0-100
  1266. $vip_info['growthvalue_percentage'] = ceil($user['growthvalue'] / $next_vip['growthvalue'] * 100);
  1267. }
  1268. //所有会员列表
  1269. $where = [];
  1270. if ($this->auth->maxlevel != 5) {
  1271. $where['id'] = ['neq', 5];
  1272. }
  1273. $list = Db::name('vip')->where($where)->select();
  1274. $vip_privilege = Db::name('vip_privilege');
  1275. foreach ($list as $k => &$v) {
  1276. $v['vip_desc'] = ''; //顶部信息
  1277. $v['growthvalue_percentage'] = 0; //成长值百分比0-100
  1278. $v['experiencetime'] = ''; //体验会员到期时间
  1279. if ($v['id'] == $user['maxlevel']) {
  1280. $v['now_level'] = 1; //是否是当前等级 1是 0否
  1281. if ($user['maxlevel'] == $user['experiencelevel'] && $user['experiencetime'] > time()) {
  1282. //实际有效会员ID是体验会员id时, 展示体验会员到期时间
  1283. $v['experiencetime'] = '有效期至' . date('Y-m-d', $user['experiencetime']);
  1284. }
  1285. } else {
  1286. $v['now_level'] = 0;
  1287. }
  1288. if ($v['id'] == $user['growthlevel']) {
  1289. $v = array_merge($v, $vip_info);
  1290. }
  1291. $v['vip_privilege'] = $vip_privilege->field('id, title, desc')
  1292. ->where(['vip_id' => $v['id']])->order('weigh desc')->select();
  1293. }
  1294. $this->success('查询所有会员', $list);
  1295. }
  1296. //意见反馈
  1297. public function feedback() {
  1298. $content = input('content', '', 'trim'); //问题描述
  1299. $images = input('images', '', 'trim'); //反馈图片,最多9张用,拼接
  1300. $mobile = input('mobile', '', 'trim'); //联系电话
  1301. $data = [];
  1302. if (!$content) {
  1303. $this->error('请输入情况说明');
  1304. }
  1305. if (iconv_strlen($content, 'utf-8') > 3000) {
  1306. $this->error('情况说明最多3000字');
  1307. }
  1308. if ($images) {
  1309. $image_arr = explode(',', $images);
  1310. if (count($image_arr) > 9) {
  1311. $this->error('附件照片最多9张');
  1312. }
  1313. $data['images'] = $images;
  1314. }
  1315. if (!is_mobile($mobile)) {
  1316. $this->error('请输入正确联系电话');
  1317. }
  1318. $data['user_id'] = $this->auth->id;
  1319. $data['content'] = $content;
  1320. $data['images'] = $images;
  1321. $data['mobile'] = $mobile;
  1322. $data['createtime'] = time();
  1323. $rs = Db::name('feedback')->insertGetId($data);
  1324. if (!$rs) {
  1325. $this->error('反馈失败');
  1326. }
  1327. $this->success('反馈成功');
  1328. }
  1329. //我的优惠券
  1330. public function mycoupon() {
  1331. $type = input('type', 0, 'intval'); //类型:1未使用 2已使用 3已过期
  1332. if (!in_array($type, [1, 2, 3])) {
  1333. $this->error('参数错误');
  1334. }
  1335. $where['user_id'] = $this->auth->id;
  1336. if ($type == 1) {
  1337. $where['endtime'] = ['egt', time()];
  1338. $where['status'] = 0;
  1339. } elseif ($type == 2) {
  1340. $where['status'] = 1;
  1341. } elseif ($type == 3) {
  1342. $where['endtime'] = ['lt', time()];
  1343. $where['status'] = 0;
  1344. }
  1345. $list = Db::name('user_coupon')->field('id, title, desc, type, money, minmoney, endtime')->where($where)->page($this->page, $this->pagenum)->select();
  1346. foreach ($list as &$v) {
  1347. $v['endtime'] = date('Y-m-d', $v['endtime']);
  1348. }
  1349. $this->success('我的优惠券', $list);
  1350. }
  1351. //充值列表
  1352. public function rechargelist() {
  1353. $list = Db::name('recharge')->field('id, title, price, coupon_id, couponnum')->order('weigh desc')->select();
  1354. $coupon = Db::name('coupon');
  1355. foreach ($list as &$v) {
  1356. $coupon_info = $coupon->find($v['coupon_id']);
  1357. if ($coupon_info) {
  1358. if ($coupon_info['type'] == 1) {
  1359. $v['coupon_title'] = $coupon_info['money'] . '折';
  1360. } else {
  1361. $v['coupon_title'] = $coupon_info['money'] . '元';
  1362. }
  1363. } else {
  1364. $v['coupon_title'] = '';
  1365. }
  1366. }
  1367. $this->success('充值列表', $list);
  1368. }
  1369. //资金明细
  1370. public function usermoneylog() {
  1371. $list = Db::name('user_money_log')->field('id, money, memo, createtime')
  1372. ->where(['user_id' => $this->auth->id])
  1373. ->page($this->page, $this->pagenum)
  1374. ->order('id desc')->select();
  1375. foreach ($list as &$v) {
  1376. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  1377. }
  1378. $this->success('资金明细', $list);
  1379. }
  1380. //充值
  1381. public function recharge() {
  1382. $id = input('id', 0, 'intval');
  1383. if (!$id) {
  1384. $this->error('参数缺失');
  1385. }
  1386. $info = Db::name('recharge')->where(['id' => $id])->find();
  1387. if (!$info) {
  1388. $this->error('充值金额不存在');
  1389. }
  1390. if ($info['price'] <= 0) {
  1391. $this->error('充值价格异常');
  1392. }
  1393. //生成支付订单记录
  1394. $rechar_order['user_id'] = $this->auth->id;
  1395. $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
  1396. $rechar_order['money'] = $info['price'];
  1397. $rechar_order['purpose'] = 2; //充值用途:1=支付订单,2=充值,3=开通会员
  1398. $rechar_order['pay_type'] = 'wechat';
  1399. $rechar_order['relation_id'] = $id;
  1400. $rechar_order['createtime'] = time();
  1401. //开始事务
  1402. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  1403. if (!$result) {
  1404. $this->error('网络延迟,请稍后再试');
  1405. }
  1406. //构建支付链接数据
  1407. $wxData['body'] = '充值';
  1408. $wxData['out_trade_no'] = $rechar_order['order_no'];
  1409. $wxData['total_fee'] = $info['price'];
  1410. // $wxData['total_fee'] = 0.01;
  1411. $wxData['openid'] = $this->auth->openid;
  1412. // require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
  1413. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  1414. $doResult = $wxPay->WxPayJs($wxData);
  1415. $this->success('微信支付参数返回成功', $doResult);
  1416. }
  1417. //邀请好友顶部信息
  1418. public function invitetop() {
  1419. $data['invite_no'] = $this->auth->invite_no; //邀请码
  1420. $data['invite_count'] = Db::name('user')->where(['pre_user_id' => $this->auth->id])->count('id'); //邀请人数
  1421. $data['coupon_count'] = Db::name('user_coupon')->where(['user_id' => $this->auth->id, 'purpose' => ['in', [2, 5]]])->count('id');
  1422. $this->success('邀请好友顶部信息', $data);
  1423. }
  1424. //邀请好友底部列表
  1425. public function invitebottom() {
  1426. $type = input('type', 0, 'intval'); //1邀请的好友 2报名的好友
  1427. if (!in_array($type, [1, 2])) {
  1428. $this->error('参数错误');
  1429. }
  1430. if ($type == 1) {
  1431. $list = Db::name('user')->field('id, avatar, nickname, invite_time')
  1432. ->where(['pre_user_id' => $this->auth->id])->page($this->page, $this->pagenum)->order('id desc')->select();
  1433. } else {
  1434. $invite_ids = Db::name('user')->where(['pre_user_id' => $this->auth->id])->column('id');
  1435. if (!$invite_ids) {
  1436. $this->success('报名好友列表', []);
  1437. }
  1438. $list = Db::name('active_order')->alias('a')
  1439. ->join('hu_user b', 'a.user_id = b.id', 'left')
  1440. ->field('b.id, b.avatar, b.nickname, b.invite_time')
  1441. ->where(['a.user_id' => ['in', $invite_ids]])
  1442. ->page($this->page, $this->pagenum)
  1443. ->order('a.id desc')->select();
  1444. }
  1445. $list = list_domain_image($list, ['avatar']);
  1446. foreach ($list as &$v) {
  1447. $v['invite_time'] = date('Y-m-d', $v['invite_time']);
  1448. }
  1449. $this->success('邀请好友底部列表', $list);
  1450. }
  1451. //我的订单
  1452. public function myorder() {
  1453. $type = input('type', 0, 'intval'); //状态:0=待付款,1=待出行,2=已完成,3=已取消
  1454. if (!in_array($type, [0, 1, 2, 3])) {
  1455. $this->error('参数错误');
  1456. }
  1457. $list = Db::name('active_order')->where(['user_id' => $this->auth->id, 'status' => $type])->page($this->page, $this->pagenum)->order('id desc')->select();
  1458. $active = Db::name('active');
  1459. foreach ($list as &$v) {
  1460. $v['button_status'] = 0;//待出行按钮: 0不展示 1退款 2修改联系人
  1461. $v['active'] = $active->find($v['active_id']);
  1462. $v['active'] = info_domain_image($v['active'], ['image']);
  1463. if ($v['status'] == 1) {
  1464. if ($v['active']['refundendtime'] >= time()) {
  1465. $v['button_status'] = 1;
  1466. } elseif ($v['active']['refundendtime'] < time() && $v['active']['signupendtime'] >= time()) {
  1467. $v['button_status'] = 2;
  1468. }
  1469. }
  1470. if ($v['active']['maxperson'] <= $v['active']['currentperson']) {
  1471. $v['is_full'] = 1; //已报满
  1472. } else {
  1473. $v['is_full'] = 0; //未报满
  1474. }
  1475. }
  1476. $this->success('我的订单', $list);
  1477. }
  1478. //我的订单详情
  1479. public function myorderinfo() {
  1480. $id = input('id', 0, 'intval'); //订单id
  1481. if (!$id) {
  1482. $this->error('参数缺失');
  1483. }
  1484. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1485. if (!$info) {
  1486. $this->error('数据不存在');
  1487. }
  1488. $info['createtime'] = date('Y-m-d H:i:s', $info['createtime']);
  1489. //查询活动信息
  1490. $active = Db::name('active')->find($info['active_id']);
  1491. //待出行按钮: 0不展示 1退款 2修改联系人
  1492. $info['button_status'] = 0;
  1493. if ($info['status'] == 1) {
  1494. if ($active['refundendtime'] >= time()) {
  1495. $info['button_status'] = 1;
  1496. } elseif ($active['refundendtime'] < time() && $active['signupendtime'] >= time()) {
  1497. $info['button_status'] = 2;
  1498. }
  1499. }
  1500. $active = info_domain_image($active, ['image']);
  1501. $active['starttime'] = date('Y-m-d H:i', $active['starttime']);
  1502. $active['endtime'] = date('Y-m-d H:i', $active['endtime']);
  1503. $active['collectiontime'] = date('Y-m-d H:i', $active['collectiontime']);
  1504. $active['signupendtime'] = date('Y-m-d H:i', $active['signupendtime']);
  1505. $active['refundendtime'] = date('Y-m-d H:i', $active['refundendtime']);
  1506. //查询报名人员
  1507. $active_people = Db::name('active_people')->where(['order_id' => $id])->field('id, name, mobile')->select();
  1508. $info['active'] = $active;
  1509. $info['active_people'] = $active_people;
  1510. $this->success('我的订单详情', $info);
  1511. }
  1512. //查看报名人信息
  1513. public function activepeople() {
  1514. $id = input('id', 0, 'intval'); //报名人员id
  1515. if (!$id) {
  1516. $this->error('参数缺失');
  1517. }
  1518. $info = Db::name('active_people')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1519. if (!$info) {
  1520. $this->error('数据不存在');
  1521. }
  1522. $this->success('查看报名人信息', $info);
  1523. }
  1524. //修改报名人信息
  1525. public function modifyactivepeople() {
  1526. $id = input('id', 0, 'intval'); //报名人员id
  1527. $collectionplace = input('collectionplace', '', 'trim'); //集合地点
  1528. $name = input('name', '', 'trim'); //姓名
  1529. $idcard = input('idcard', '', 'trim'); //身份证号
  1530. $mobile = input('mobile', '', 'trim'); //手机号
  1531. $emergencycontact = input('emergencycontact', '', 'trim'); //紧急联系人
  1532. $contactmobile = input('contactmobile', '', 'trim'); //紧急联系方式
  1533. if (!$id) {
  1534. $this->error('参数缺失');
  1535. }
  1536. $info = Db::name('active_people')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1537. if (!$info) {
  1538. $this->error('数据不存在');
  1539. }
  1540. if ($info['status'] == 2) {
  1541. $this->success('订单已经结束');
  1542. }
  1543. if ($info['status'] == 3) {
  1544. $this->success('订单已经取消');
  1545. }
  1546. if ($info['modifystatus'] == 1) {
  1547. $this->error('报名信息正在修改中,请等待后台审核');
  1548. }
  1549. //查询活动状态
  1550. $active = Db::name('active')->find($info['active_id']);
  1551. if (!$active) {
  1552. $this->success('活动不存在或已取消');
  1553. }
  1554. // if ($active['status'] == 1) {
  1555. // $this->error('活动已成行,不能修改信息');
  1556. // }
  1557. if (time() <= $active['refundendtime'] || time() > $active['signupendtime']) {
  1558. $this->error('当前时间段暂不可修改信息');
  1559. }
  1560. //查询是否申请过退款
  1561. $active_refund = Db::name('active_refund')->where(['order_id' => $info['active_id'], 'status' => 0])->count('id');
  1562. if ($active_refund) {
  1563. $this->error('您正在申请退款,暂不可修改信息');
  1564. }
  1565. //检查信息
  1566. if ($collectionplace === '' || iconv_strlen($collectionplace, 'utf-8') > 255) {
  1567. $this->error('请选择集合地点');
  1568. }
  1569. if (!$name || iconv_strlen($name, 'utf-8') > 50) {
  1570. $this->error('请输入正确姓名');
  1571. }
  1572. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1573. $this->error('请输入正确身份证号');
  1574. }
  1575. if (!is_mobile($mobile)) {
  1576. $this->error('请输入正确手机号');
  1577. }
  1578. if (!$emergencycontact || iconv_strlen($emergencycontact, 'utf-8') > 50) {
  1579. $this->error('请输入紧急联系人');
  1580. }
  1581. if (!is_mobile($contactmobile)) {
  1582. $this->error('请输入正确紧急联系人方式');
  1583. }
  1584. //判断是否报名过
  1585. $count = Db::name('active_people')->where(['active_id' => $info['active_id'], 'idcard' => $idcard, 'status' => ['neq', 3]])->count('id');
  1586. if ($count) {
  1587. $this->error('该信息已报名过活动');
  1588. }
  1589. $count2 = Db::name('active_people_modify')->where(['active_id' => $info['active_id'], 'idcard' => $idcard, 'status' => 0])->count('id');
  1590. if ($count2) {
  1591. $this->error('该信息已提交过修改,请等待审核');
  1592. }
  1593. //构建修改数据
  1594. $data = [
  1595. 'active_id' => $info['active_id'],
  1596. 'order_id' => $info['order_id'],
  1597. 'people_id' => $id,
  1598. 'user_id' => $info['user_id'],
  1599. 'collectionplace' => $collectionplace,
  1600. 'name' => $name,
  1601. 'credtype' => $info['credtype'],
  1602. 'idcard' => $idcard,
  1603. 'mobile' => $mobile,
  1604. 'emergencycontact' => $emergencycontact,
  1605. 'contactmobile' => $contactmobile,
  1606. 'insurance' => $info['insurance'],
  1607. 'originalprice' => $info['originalprice'],
  1608. 'vipprice' => $info['vipprice'],
  1609. 'coupon_id' => $info['coupon_id'],
  1610. 'coupontype' => $info['coupontype'],
  1611. 'couponprice' => $info['couponprice'],
  1612. 'is_free' => $info['is_free'],
  1613. 'price' => $info['price'],
  1614. 'is_self' => $info['is_self'],
  1615. 'createtime' => time()
  1616. ];
  1617. //开启事务
  1618. Db::startTrans();
  1619. //添加记录
  1620. $rs = Db::name('active_people_modify')->insertGetId($data);
  1621. if (!$rs) {
  1622. Db::rollback();
  1623. $this->error('修改失败');
  1624. }
  1625. //修改原记录状态
  1626. $rt = Db::name('active_people')->where(['id' => $id, 'modifystatus' => 0])->setField('modifystatus', 1);
  1627. if (!$rt) {
  1628. Db::rollback();
  1629. $this->error('修改失败');
  1630. }
  1631. Db::commit();
  1632. $this->success('修改成功,请等待审核');
  1633. }
  1634. //取消订单
  1635. public function cancelorder() {
  1636. $id = input('id', 0, 'intval'); //订单id
  1637. if (!$id) {
  1638. $this->error('参数缺失');
  1639. }
  1640. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1641. if (!$info) {
  1642. $this->error('数据不存在');
  1643. }
  1644. if ($info['status'] != 0) {
  1645. $this->error('网络延迟,请刷新重试');
  1646. }
  1647. if ($info['createtime'] + 1800 < time()) {
  1648. $this->error('订单超时,系统将自动取消');
  1649. }
  1650. //查询活动是否存在
  1651. $active_info = Db::name('active')->find($info['active_id']);
  1652. if (!$active_info) {
  1653. $this->error('活动不存在');
  1654. }
  1655. //开启事务
  1656. Db::startTrans();
  1657. //修改订单信息
  1658. $rs = Db::name('active_order')->where(['id' => $id, 'status' => 0])->setField('status', 3);
  1659. if (!$rs) {
  1660. Db::rollback();
  1661. $this->error('网络延迟,请稍后再试');
  1662. }
  1663. //修改活动人员
  1664. $rt = Db::name('active_people')->where(['order_id' => $id, 'status' => 0])->setField(['status' => 3, 'modifystatus' => 0]);
  1665. if (!$rt) {
  1666. Db::rollback();
  1667. $this->error('网络延迟,请稍后再试');
  1668. }
  1669. //修改活动人员修改记录
  1670. $res = Db::name('active_people_modify')->where(['order_id' => $id, 'status' => 0])->setField('status', 2);
  1671. if ($res === false) {
  1672. Db::rollback();
  1673. $this->error('网络延迟,请稍后再试');
  1674. }
  1675. //减少活动已报名人数
  1676. $currentperson = $active_info['currentperson'] - $info['number'];
  1677. $active_rs = Db::name('active')->where(['id' => $info['active_id'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $currentperson);
  1678. if (!$active_rs) {
  1679. Db::rollback();
  1680. $this->error('网络延迟,请稍后再试');
  1681. }
  1682. Db::commit();
  1683. $this->success('修改成功');
  1684. }
  1685. //支付订单
  1686. public function payorder() {
  1687. $id = input('id', 0, 'intval'); //订单id
  1688. if (!$id) {
  1689. $this->error('参数缺失');
  1690. }
  1691. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1692. if (!$info) {
  1693. $this->error('数据不存在');
  1694. }
  1695. if ($info['status'] != 0) {
  1696. $this->error('当前订单状态不能支付');
  1697. }
  1698. if ($info['createtime'] + 1800 < time()) {
  1699. $this->error('订单支付超时,请重新报名');
  1700. }
  1701. //查询活动是否存在
  1702. $active_info = Db::name('active')->find($info['active_id']);
  1703. if (!$active_info) {
  1704. $this->error('活动不存在');
  1705. }
  1706. if ($active_info['status'] == 2) {
  1707. $this->error('活动已经结束');
  1708. }
  1709. if ($active_info['status'] == 3) {
  1710. $this->error('活动已取消');
  1711. }
  1712. $pay_count = Db::name('rechar_order')->where(['user_id' => $this->auth->id, 'purpose' => 1, 'pay_type' => 'wechat', 'relation_id' => $id, 'status' => 1])->count('id');
  1713. if ($pay_count) {
  1714. $this->error('您已成功支付过,请勿重复支付');
  1715. }
  1716. //生成支付订单记录
  1717. $rechar_order['user_id'] = $this->auth->id;
  1718. $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
  1719. $rechar_order['money'] = $info['price'];
  1720. $rechar_order['purpose'] = 1; //充值用途:1=支付订单,2=充值,3=开通会员
  1721. $rechar_order['pay_type'] = 'wechat';
  1722. $rechar_order['relation_id'] = $id;
  1723. $rechar_order['createtime'] = time();
  1724. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  1725. if (!$result) {
  1726. $this->error('网络延迟,请稍后再试');
  1727. }
  1728. //构建支付链接数据
  1729. $wxData['body'] = '报名活动支付';
  1730. $wxData['out_trade_no'] = $rechar_order['order_no'];
  1731. $wxData['total_fee'] = $info['price'];
  1732. // $wxData['total_fee'] = 0.01;
  1733. $wxData['openid'] = $this->auth->openid;
  1734. // require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
  1735. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  1736. $doResult = $wxPay->WxPayJs($wxData);
  1737. $this->success('微信支付参数返回成功', $doResult);
  1738. }
  1739. //申请退款
  1740. public function applyrefund() {
  1741. $id = input('id', 0, 'intval'); //订单id
  1742. if (!$id) {
  1743. $this->error('参数缺失');
  1744. }
  1745. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1746. if (!$info) {
  1747. $this->error('数据不存在');
  1748. }
  1749. if ($info['status'] != 1) {
  1750. $this->error('当前订单状态不能退款');
  1751. }
  1752. //查询是否已经申请过
  1753. $count = Db::name('active_refund')->where(['order_id' => $id, 'status' => 0])->count('id');
  1754. if ($count) {
  1755. $this->error('您已申请过退款,请等待后台审核');
  1756. }
  1757. //查询活动是否存在
  1758. $active_info = Db::name('active')->find($info['active_id']);
  1759. if (!$active_info) {
  1760. $this->error('活动不存在');
  1761. }
  1762. if (time() > $active_info['refundendtime']) {
  1763. $this->error('已超过退款截止时间,暂不能退款');
  1764. }
  1765. //用户取消订单每人扣费金额(元)
  1766. $cancelorder_price = config('site.cancelorder');
  1767. if ($cancelorder_price < 0) {
  1768. $this->error('退款手续费异常,请联系管理员');
  1769. }
  1770. //退款金额
  1771. $refundprice = number_format($info['price'] - $cancelorder_price * $info['number'], 2, '.', '');
  1772. $refundprice = $refundprice > 0 ? $refundprice : 0;
  1773. //构建申请数据
  1774. $data = [
  1775. 'active_id' => $info['active_id'],
  1776. 'order_id' => $id,
  1777. 'user_id' => $this->auth->id,
  1778. 'paytype' => $info['paytype'],
  1779. 'price' => $info['price'],
  1780. 'number' => $info['number'],
  1781. 'transaction_id' => $info['transaction_id'],
  1782. 'refund_no' => $info['paytype'] ? date('YmdHis', time()) . rand(10000000, 99999999) : '', //退款单号
  1783. 'refundprice' => $refundprice,
  1784. 'createtime' => time()
  1785. ];
  1786. $rs = Db::name('active_refund')->insertGetId($data);
  1787. if (!$rs) {
  1788. $this->error('申请退款失败');
  1789. }
  1790. $this->success('申请退款成功,请等待审核');
  1791. }
  1792. //查询用户信息
  1793. public function getmyinfo() {
  1794. $user_id = input('user_id', 0, 'intval'); //用户id
  1795. if (!$user_id) {
  1796. $user_id = $this->auth->id;
  1797. }
  1798. $hu_user = Db::name('user');
  1799. $user_info = $hu_user->field(['id', 'nickname', 'mobile', 'avatar', 'height', 'weight', 'birthday', 'excel_position', 'rank_id', 'total_num', 'win_num', 'lose_num', 'power_value', 'mvp_num', 'foul_num', 'block_num', 'help_num', 'backboard_num', 'score', 'assess'])->where(['id' => $user_id])->find();
  1800. if (!$user_info) {
  1801. $this->error('您的网络开小差了');
  1802. }
  1803. $user_info['avatar'] = one_domain_image($user_info['avatar']);
  1804. $user_info['age'] = birthtime_to_age($user_info['birthday']);
  1805. $user_info['birthday'] = date('Y-m-d', $user_info['birthday']);
  1806. $rank_info = Db::name('rank')->where(['id' => $user_info['rank_id']])->find();
  1807. $user_info['rank_name'] = $rank_info ? $rank_info['name'] : '';
  1808. $user_info['rank_image'] = $rank_info ? one_domain_image($rank_info['image']) : '';
  1809. $user_info['max_total_num'] = $hu_user->order('total_num desc')->value('total_num');
  1810. $user_info['max_power_value'] = $hu_user->order('power_value desc')->value('power_value');
  1811. $user_info['max_win_num'] = $hu_user->order('win_num desc')->value('win_num');
  1812. $user_info['max_mvp_num'] = $hu_user->order('mvp_num desc')->value('mvp_num');
  1813. $user_info['max_foul_num'] = $hu_user->order('foul_num desc')->value('foul_num');
  1814. $user_info['winning_rate'] = $user_info['total_num'] == 0 ? 0 : ceil($user_info['win_num'] / $user_info['total_num']);
  1815. $user_info['max_block_num'] = $hu_user->order('block_num desc')->value('block_num');
  1816. $user_info['max_help_num'] = $hu_user->order('help_num desc')->value('help_num');
  1817. $user_info['max_backboard_num'] = $hu_user->order('backboard_num desc')->value('backboard_num');
  1818. $user_info['max_score'] = $hu_user->order('score desc')->value('score');
  1819. $this->success('信息', $user_info);
  1820. }
  1821. //修改信息
  1822. public function editinfo() {
  1823. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //头像
  1824. $nickname = $this->request->post('nickname', '', 'trim'); //昵称
  1825. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  1826. $height = input('height', 0, 'intval'); //身高
  1827. $weight = input('weight', 0, 'intval'); //体重
  1828. $excel_position = input('excel_position', '', 'trim'); //擅长位置
  1829. $data = [];
  1830. if ($avatar) {
  1831. if (iconv_strlen($avatar, 'utf-8') > 255) {
  1832. $this->error('图片超出范围');
  1833. }
  1834. $data['avatar'] = $avatar;
  1835. }
  1836. if ($nickname) {
  1837. if (iconv_strlen($nickname, 'utf-8') > 30) {
  1838. $this->error('昵称最多30字');
  1839. }
  1840. $data['nickname'] = $nickname;
  1841. }
  1842. if ($height > 0) {
  1843. $data['height'] = $height;
  1844. }
  1845. if ($weight > 0) {
  1846. $data['weight'] = $weight;
  1847. }
  1848. if ($birthday) {
  1849. $data['birthday'] = $birthday;
  1850. }
  1851. if ($excel_position) {
  1852. if(iconv_strlen($excel_position, 'utf-8') > 30) {
  1853. $this->error('请选择正确位置');
  1854. }
  1855. $data['excel_position'] = $excel_position;
  1856. }
  1857. if (!$data) {
  1858. $this->error('暂无修改内容');
  1859. }
  1860. //修改用户表
  1861. $rt = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1862. if ($rt === false) {
  1863. $this->error('修改失败');
  1864. }
  1865. $this->success('修改成功');
  1866. }
  1867. //我的比赛
  1868. public function mygame() {
  1869. $list = Db::name('game_people')->field('id, game_id, createtime, status')->where(['user_id' => $this->auth->id, 'status' => ['gt', 0]])->page($this->page, $this->pagenum)->order('id desc')->select();
  1870. if ($list) {
  1871. $hu_game = Db::name('game');
  1872. foreach ($list as &$v) {
  1873. $v['createtime'] = date('Y.m.d', $v['createtime']);
  1874. $game_info = $hu_game->field('name, image')->where(['id' => $v['game_id']])->find();
  1875. $v['game_name'] = $game_info['name'];
  1876. $v['game_image'] = one_domain_image($game_info['image']);
  1877. }
  1878. }
  1879. $this->success('我的比赛', $list);
  1880. }
  1881. //取消报名
  1882. public function cancelsignup() {
  1883. $id = input('id', 0, 'intval'); //报名id
  1884. if (!$id) {
  1885. $this->error('参数缺失');
  1886. }
  1887. $game_people_info = Db::name('game_people')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1888. if (!$game_people_info) {
  1889. $this->error('数据丢失');
  1890. }
  1891. if ($game_people_info['status'] == 0) {
  1892. $this->error('您尚未报名');
  1893. }
  1894. if ($game_people_info['status'] != 1) {
  1895. $this->error('您已取消报名了');
  1896. }
  1897. $time = time();
  1898. if ($game_people_info['starttime'] < $time) {
  1899. $this->error('比赛已经开始,不能取消');
  1900. }
  1901. $data['group'] = 0;
  1902. $data['is_leader'] = 0;
  1903. $data['now_num'] = $game_people_info['is_leader'] == 1 ? 0 : ($game_people_info['now_num'] - 1);
  1904. $data['status'] = 2;
  1905. $data['updatetime'] = $time;
  1906. //开启事务
  1907. Db::startTrans();
  1908. //修改数据
  1909. $rs = Db::name('game_people')->where(['id' => $id, 'status' => 1])->setField($data);
  1910. if (!$rs) {
  1911. Db::rollback();
  1912. $this->error('您的网络开小差了');
  1913. }
  1914. if ($game_people_info['is_leader'] == 1) {
  1915. //是队长, 取消即解散队伍
  1916. //开启redis
  1917. $redis = new \Redis();
  1918. $redis->connect(config('redis_host'), config('redis_port'));
  1919. if (config('redis_pwd')) {
  1920. $redis->auth(config('redis_pwd'));
  1921. }
  1922. if(config('redis_selectdb') > 0){
  1923. $redis->select(config('redis_selectdb'));
  1924. }
  1925. //是否可以操作: 1可以 其他 不可以
  1926. $game_people_status = $redis->lPop('game_people' . $game_people_info['game_id'] . '_' . $game_people_info['group']);
  1927. if ($game_people_status != 1) {
  1928. Db::rollback();
  1929. $this->error('网络繁忙,请稍后再试');
  1930. }
  1931. //更改队伍其他成员信息
  1932. $rt = Db::name('game_people')->where(['game_id' => $game_people_info['game_id'], 'group' => $game_people_info['group']])->setField(['group' => 0, 'now_num' => 0, 'updatetime' => $time]);
  1933. if ($rt === false) {
  1934. Db::rollback();
  1935. $redis->rPush('game_people' . $game_people_info['game_id'] . '_' . $game_people_info['group'], 1);
  1936. $this->error('您的网络开小差了');
  1937. }
  1938. $redis->rPush('game_people' . $game_people_info['game_id'] . '_' . $game_people_info['group'], 1);
  1939. }
  1940. Db::commit();
  1941. $this->success('取消成功');
  1942. }
  1943. //联系我们
  1944. public function contact() {
  1945. $data['customer_service'] = config('site.customer_service'); //客服电话
  1946. $data['customer_service_desc'] = config('site.customer_service_desc'); //客服电话描述
  1947. $data['qq'] = config('site.qq'); //联系QQ
  1948. $data['qr_code'] = cdnurl(config('site.qr_code')); //微信群二维码
  1949. $this->success('联系我们', $data);
  1950. }
  1951. }