User.php 70 KB

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