Index.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Banner;
  4. use app\api\model\Category;
  5. use app\api\model\CoinRecord;
  6. use app\api\model\Detail;
  7. use app\api\model\Goods;
  8. use app\api\model\MoneyRecord;
  9. use app\api\model\Order;
  10. use app\api\model\PriceRange;
  11. use app\api\model\Prizerecord;
  12. use app\api\model\RechargeList;
  13. use app\api\model\RechargeOrder;
  14. use app\api\model\SearchHistory;
  15. use app\api\model\Setting;
  16. use app\api\model\Box;
  17. use app\api\model\Star;
  18. use app\api\model\Text;
  19. use app\common\controller\Api;
  20. use think\Db;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\Exception;
  24. use think\exception\DbException;
  25. use app\api\library\Retail;
  26. use think\Validate;
  27. /**
  28. * 首页接口
  29. */
  30. class Index extends Api
  31. {
  32. protected $noNeedLogin = ['baseInfo', 'newestOpen', 'hotBox', 'cheapBox', 'recommend', 'categoryList',
  33. 'boxList', 'boxListByCategory', 'boxDetail', 'boxOpenRecord', 'tryBoxDetail', 'priceRange', 'rechargeList', 'getServiceInfo',
  34. 'getWechatLoginUrl', "getVirData", "sumUv", "getServiceurl","lunbobox",'boximages'];
  35. protected $noNeedRight = ['*'];
  36. protected $lockUserId = ['coinPay'];
  37. public function sumUv()
  38. {
  39. $ip = request()->ip();
  40. $check = db("uv")->where(['ip'=>$ip])->whereTime("createtime", "today")->find();
  41. if(empty($check)){
  42. db("uv")->insert(['ip'=>$ip,"createtime"=>time()]);
  43. }
  44. $this->success("记录成功");
  45. }
  46. /**
  47. *获取开盒假数据
  48. */
  49. public function getVirData()
  50. {
  51. $page = request()->param("page")??1;
  52. $limit = request()->param("limit")??10;
  53. $domain = request()->domain();
  54. $list = db("virtuial")->alias("a")->join("goods b","b.id = a.goods_id")->order("a.add_time desc")->field("a.*,FROM_UNIXTIME(a.add_time,'%Y-%m-%d %H:%i') createtime,b.goods_name, b.image,b.coin_price price")->page($page, $limit)->select();
  55. foreach($list as $k=>$v){
  56. $list[$k]['avatar'] = $domain.$v['avatar'];
  57. //$list[$k]['image'] = $domain.$v['image'];
  58. $list[$k]['image'] = $v['image'];
  59. }
  60. $total = db("virtuial")->order("add_time desc")->count();
  61. // print_r($list);die;
  62. $this->success("获取成功", ["list"=>$list,"total"=>$total]);
  63. }
  64. public function lunbobox(){
  65. $status = 1;
  66. $pagesize = input('pagesize/d', 10);
  67. $page = input('page/d', 1);
  68. $statusList = [1 => 'bag', 2 => 'exchange'];
  69. if (!isset($statusList[$status])) {
  70. $this->error('状态有误');
  71. }
  72. $status = $statusList[$status];
  73. $order = 'prize.id desc';
  74. if ('exchange' == $status) {
  75. $order = 'exchange_time desc';
  76. }
  77. $list = Prizerecord::alias('prize')
  78. ->distinct(true)
  79. ->field('prize.goods_name,prize.goods_image')
  80. ->field('prize.box_id')
  81. ->field('user.nickname,user.avatar')
  82. ->join('user user', 'user.id = prize.user_id')
  83. ->order($order)
  84. ->limit(20)
  85. ->select();
  86. $this->success('查询成功', $list);
  87. }
  88. /**
  89. * 基础信息
  90. * @author fuyelk <fuyelk@fuyelk.com>
  91. */
  92. public function baseInfo()
  93. {
  94. // 查询设置
  95. $setting = Setting::get(1);
  96. // 查询轮播图
  97. $banner = Banner::where('place', 'index')->order('weigh', 'desc')->select();
  98. $bannerlist = [];
  99. foreach ($banner as $item) {
  100. $bannerlist[] = [
  101. 'type' => $item->type,
  102. 'image' => cdnurl($item->image, true),
  103. 'value' => $item->value
  104. ];
  105. }
  106. $ret = [
  107. 'logo' => cdnurl($setting->logo_image, true), // 首页LOGO
  108. 'banner' => $bannerlist
  109. ];
  110. $this->success('查询成功', $ret);
  111. }
  112. public function sList()
  113. {
  114. $status = input('status/d');
  115. $pagesize = input('pagesize/d', 10);
  116. $page = input('page/d', 1);
  117. $statusList = [1 => 'bag', 2 => 'exchange'];
  118. if (!isset($statusList[$status])) {
  119. $this->error('状态有误');
  120. }
  121. $status = $statusList[$status];
  122. $order = 'prize.id desc';
  123. if ('exchange' == $status) {
  124. $order = 'exchange_time desc';
  125. }
  126. $order= Db::table('box_shai')->where('switch','1') ->order('create_time', 'desc')->limit(($page-1)*$pagesize,$pagesize)->select();
  127. $list2 = array(
  128. 'id'=>1,
  129. 'stx'=>'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic3.zhimg.com%2F50%2Fv2-6f1c492cbdfe3c24aae44e935a796d5a_hd.jpg&refer=http%3A%2F%2Fpic3.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1632275904&t=20baefb74a9f7b152b601ec58726f1e7',
  130. 'smc'=>'这个是名称',
  131. 'ssj'=>'前天',
  132. 'szw'=>'这个盲盒很好玩,我很喜欢,我也介绍了很多人过来完',
  133. 'simg'=>array(
  134. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  135. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  136. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  137. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  138. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  139. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  140. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  141. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  142. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  143. )
  144. );
  145. /* ->each(function ($item) use ($status) {
  146. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  147. $item->box_coin_price = intval($item->box_coin_price);
  148. $item->box_rmb_price = floatval($item->box_rmb_price);
  149. if ('exchange' == $status) {
  150. $item->time = date('Y-m-d H:i:s', $item->exchange_time);
  151. } else {
  152. $item->time = date('Y-m-d H:i:s', $item->create_time);
  153. }
  154. $item->hidden(['create_time', 'exchange_time']);
  155. });
  156. */
  157. //print_r($list);
  158. //exit;
  159. $ym='https://'. $_SERVER['HTTP_HOST'];
  160. foreach ($order as $k=>$v){
  161. // $order[$k]['box_tx']=$ym.$v['box_tx'];
  162. $order[$k]['box_img']=explode(',',$v['box_img']);
  163. // foreach ($order[$k]['box_img'] as $k2=>$v2){
  164. // $order[$k]['box_img'][$k2]=$ym.$v2;
  165. // }
  166. if(time()-$v['create_time']<60*60){
  167. $t=round((time()-$v['create_time'])/60);
  168. if($t==0){$t=1;}
  169. $order[$k]['box_sj']=$t.'分钟前';
  170. }
  171. else if (time()-$v['create_time']>60*60&&time()-$v['create_time']<60*60*24){
  172. $t=round((time()-$v['create_time'])/60/60);
  173. $order[$k]['box_sj']=$t.'小时前';
  174. }
  175. else if (time()-$v['create_time']>60*60*24&&time()-$v['create_time']<60*60*24*7){
  176. $t=round((time()-$v['create_time'])/60/60/24);
  177. $order[$k]['box_sj']=$t.'天前';
  178. }
  179. else if (time()-$v['create_time']>60*60*24*7){
  180. $t=round((time()-$v['create_time'])/60/60/24/7);
  181. $order[$k]['box_sj']=$t.'周前';
  182. }
  183. }
  184. //print_r($order);
  185. $list=array(
  186. 'data'=> $order
  187. );
  188. //$list=$order;
  189. foreach ($list as $k=>$v){
  190. // print_r($v);
  191. }
  192. $this->success('查询成功', $list);
  193. }
  194. /**
  195. * 最新开箱的盲盒列表
  196. * @author fuyelk <fuyelk@fuyelk.com>
  197. */
  198. public function newestOpen()
  199. {
  200. // 查询数量
  201. $limit = 5;
  202. // 查询用户最新购买的100个盲盒ID
  203. $boxIds = Order::field('box_id')->order('id', 'desc')->limit(100)->column('box_id');
  204. //Array ( [0] => 36 [1] => 37 [2] => 38 [3] => 39 [4] => 40 [5] => 36 [6] => 36 [7] => 36 [8] => 36 [9] => 41 [10] => 39 [11] => 39 [12] => 36 [13] => 37 [14] => 41 [15] => 41 )
  205. $boxIds = array_unique($boxIds);
  206. $boxAdd = [];
  207. // 新开箱不足5个,则按盲盒上架顺序补齐
  208. if (count($boxIds) < $limit) {
  209. $boxAdd = Box::whereNotIn('id', $boxIds)->order('update_time', 'desc')->limit($limit - count($boxIds))->column('id');
  210. }
  211. //Array ( [0] => 36 [1] => 37 [2] => 38 [3] => 39 [4] => 40 [5] => 41 )
  212. $boxIds = array_merge($boxIds, $boxAdd);
  213. // 查询空盲盒
  214. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  215. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  216. $list = Box::alias('box')
  217. ->field('id box_id,box_name,coin_price,box_banner_images')
  218. ->whereNotIn('id', $emptyBoxIds)
  219. ->whereIn('id', $boxIds)
  220. ->order('id', 'desc')
  221. ->select();
  222. //print_r($list);die;
  223. foreach ($list as $item) {
  224. // 查询前5个商品图片
  225. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(4)->column('goods_id');
  226. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  227. foreach ($goods_images as &$image) {
  228. $image = cdnurl($image, true);
  229. }
  230. // 查询最低价
  231. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  232. $item->goods_num = count($allGoods);
  233. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  234. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  235. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  236. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  237. $item->goods_images = $goods_images;
  238. }
  239. //print_r($list);die;
  240. $this->success('查询成功', $list);
  241. }
  242. public function boximages()
  243. {
  244. $box_id = input('box_id');
  245. $tagName = [
  246. 'normal' => '普通',
  247. 'rare' => '稀有',
  248. 'supreme' => '史诗',
  249. 'legend' => '传说',
  250. ];
  251. // $firstGoods = db('box_detail')->where('box_id', 49)->find();
  252. // 查询商品id及概率
  253. $detail = db('box_detail')->where('box_id', $box_id)->order('weigh', 'desc')->column('goods_id');
  254. // 查询全部商品
  255. $moreGoods = db('goods')->field('image')
  256. ->where('status', 'online')
  257. ->whereIn('id', $detail)
  258. ->select();
  259. $ret = [
  260. 'goodsimagelist' => $moreGoods,
  261. ];
  262. $this->success('查询成功', $ret);
  263. }
  264. /**
  265. * 热门盲盒
  266. * @author fuyelk <fuyelk@fuyelk.com>
  267. */
  268. public function hotBox()
  269. {
  270. $pagesize = input('pagesize/d', 10);
  271. $page = input('page/d', 1);
  272. // 查询空盲盒
  273. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  274. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  275. $list = Box::alias('box')
  276. ->field('id box_id,box_name,coin_price')
  277. ->where('is_hot', 1)
  278. ->whereNotIn('box.id', $emptyBoxIds)
  279. ->order('box.id', 'desc')
  280. ->paginate($pagesize, false, ['page' => $page])
  281. ->each(function ($item) {
  282. // 查询前6个商品图片
  283. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  284. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  285. foreach ($goods_images as &$image) {
  286. $image = cdnurl($image, true);
  287. }
  288. // 查询最低价
  289. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  290. $item->goods_num = count($allGoods);
  291. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  292. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  293. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  294. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  295. $item->goods_images = $goods_images;
  296. });
  297. $ret = [
  298. 'banner' => cdnurl(Setting::getSetting('hot_box_banner'), true),
  299. 'list' => $list
  300. ];
  301. $this->success('查询成功', $ret);
  302. }
  303. /**
  304. * 低价盲盒
  305. * @author fuyelk <fuyelk@fuyelk.com>
  306. */
  307. public function cheapBox()
  308. {
  309. $pagesize = input('pagesize/d', 10);
  310. $page = input('page/d', 1);
  311. // 查询空盲盒
  312. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  313. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  314. $list = Box::alias('box')
  315. ->field('id box_id,box_name,coin_price')
  316. ->where('is_cheap', 1)
  317. ->whereNotIn('box.id', $emptyBoxIds)
  318. ->order('box.id', 'desc')
  319. ->paginate($pagesize, false, ['page' => $page])
  320. ->each(function ($item) {
  321. // 查询前6个商品图片
  322. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  323. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  324. foreach ($goods_images as &$image) {
  325. $image = cdnurl($image, true);
  326. }
  327. // 查询最低价
  328. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  329. $item->goods_num = count($allGoods);
  330. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  331. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  332. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  333. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  334. $item->goods_images = $goods_images;
  335. });
  336. $ret = [
  337. 'banner' => cdnurl(Setting::getSetting('cheap_box_banner'), true),
  338. 'list' => $list
  339. ];
  340. $this->success('查询成功', $ret);
  341. }
  342. /**
  343. * 推荐盲盒
  344. * @throws \think\Exception
  345. * @throws \think\db\exception\DataNotFoundException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. * @throws \think\exception\DbException
  348. * @author fuyelk <fuyelk@fuyelk.com>
  349. */
  350. public function recommend()
  351. {
  352. $pagesize = input('pagesize/d', 10);
  353. $page = input('page/d', 1);
  354. // 查询空盲盒
  355. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  356. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  357. $list = Box::alias('box')
  358. ->field('id box_id,box_name,coin_price,box_banner_images,box_foot_images')
  359. ->whereNotIn('id', $emptyBoxIds)
  360. ->order('sort', 'asc')
  361. ->where("switch",1)
  362. ->paginate($pagesize, false, ['page' => $page])
  363. ->each(function ($item) {
  364. // 查询前6个商品图片
  365. // $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  366. // $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  367. $goods_images = Detail::alias('a')->join("goods b","b.id = a.goods_id")->where('a.box_id', $item->box_id)->order('a.weigh', 'desc')->limit(6)->column('b.image');
  368. foreach ($goods_images as &$image) {
  369. $image = cdnurl($image, true);
  370. }
  371. // 查询最低价
  372. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  373. $item->goods_num = count($allGoods);
  374. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  375. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  376. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  377. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  378. $item->goods_images = $goods_images;
  379. });
  380. $this->success('查询成功', $list);
  381. }
  382. /**
  383. * 分类列表
  384. * @author fuyelk <fuyelk@fuyelk.com>
  385. */
  386. public function categoryList()
  387. {
  388. $list = Category::field('id category_id,name')->order('weigh', 'desc')->select();
  389. $this->success('查询成功', $list);
  390. }
  391. /**
  392. * 盲盒列表
  393. * @throws \think\exception\DbException
  394. * @author fuyelk <fuyelk@fuyelk.com>
  395. */
  396. public function boxListByCategory()
  397. {
  398. $pagesize = input('pagesize/d', 10);
  399. $page = input('page/d', 1);
  400. $category_id = input('category_id/d');
  401. $search = input('search', '');
  402. $sort = input('sort', '');
  403. $price = input('price', '');
  404. // 构建筛选条件
  405. $wherePrice = [];
  406. if ($price) {
  407. list($price_low, $price_heigh) = array_slice(explode('-', $price), 0, 2);
  408. if (!is_numeric($price_low) || !is_numeric($price_heigh)) {
  409. $this->error('价格筛选有误');
  410. }
  411. $wherePrice = ['coin_price' => ['between', [$price_low, $price_heigh]]];
  412. }
  413. // 构建排序条件
  414. $order = "";
  415. switch ($sort) {
  416. case '':
  417. $order = "id desc";
  418. break;
  419. case 'new':
  420. $order = "id desc";
  421. break;
  422. case 'combine_desc':
  423. $order = "sales desc";
  424. break;
  425. case 'combine_asc':
  426. $order = "sales asc";
  427. break;
  428. case 'price_desc':
  429. $order = "coin_price desc";
  430. break;
  431. case 'price_asc':
  432. $order = "coin_price asc";
  433. break;
  434. default:
  435. $this->error('排序方式有误');
  436. break;
  437. }
  438. if (empty($category_id) && empty($search)) {
  439. $this->error('查询条件有误');
  440. }
  441. $whereCategory = [];
  442. if (!empty($category_id)) {
  443. $whereCategory = ['category_id' => $category_id];
  444. }
  445. $whereSearch = [];
  446. if (!empty($search)) {
  447. // 过滤非法字符
  448. $search = preg_replace("/[!@#$%^&*=\-+_\[\]{};':\"\/<>?]+/", '', $search);
  449. // 创建搜索记录
  450. if ($this->auth->isLogin()) {
  451. if (!SearchHistory::where(['user_id' => $this->auth->id, 'search' => $search])->value('id')) {
  452. SearchHistory::create(['user_id' => $this->auth->id, 'search' => $search]);
  453. }
  454. }
  455. $whereSearch = "box_name like '%{$search}%'";
  456. }
  457. // 查询空盲盒
  458. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  459. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  460. $list = Box::field('id box_id,box_name,coin_price,box_banner_images')
  461. ->order($order)
  462. ->whereNotIn('id', $emptyBoxIds)
  463. ->where($wherePrice)
  464. ->where($whereCategory)
  465. ->where($whereSearch)
  466. ->paginate($pagesize, false, ['page' => $page])
  467. ->each(function ($item) {
  468. // 查询第一张图片
  469. $goodsid = Detail::order('weigh', 'desc')->where('box_id', $item->box_id)->value('goods_id');
  470. $item->image = '';
  471. if ($goodsid) {
  472. $image = Goods::where('id', $goodsid)->value('image');
  473. $item->image = $image ? cdnurl($image, true) : '';
  474. }
  475. });
  476. $this->success('查询成功', $list);
  477. }
  478. /**
  479. * 搜索
  480. * @throws DbException
  481. * @author fuyelk <fuyelk@fuyelk.com>
  482. */
  483. public function search()
  484. {
  485. $pagesize = input('pagesize/d', 10);
  486. $page = input('page/d', 1);
  487. $category_id = input('category_id/d');
  488. $search = input('search', '');
  489. $sort = input('sort', '');
  490. $price = input('price', '');
  491. // 构建筛选条件
  492. $wherePrice = [];
  493. if ($price) {
  494. list($price_low, $price_heigh) = array_slice(explode('-', $price), 0, 2);
  495. if (!is_numeric($price_low) || !is_numeric($price_heigh)) {
  496. $this->error('价格筛选有误');
  497. }
  498. $wherePrice = ['coin_price' => ['between', [$price_low, $price_heigh]]];
  499. }
  500. // 构建排序条件
  501. $order = "";
  502. switch ($sort) {
  503. case '':
  504. $order = "id desc";
  505. break;
  506. case 'new':
  507. $order = "id desc";
  508. break;
  509. case 'combine_desc':
  510. $order = "sales desc";
  511. break;
  512. case 'combine_asc':
  513. $order = "sales asc";
  514. break;
  515. case 'price_desc':
  516. $order = "coin_price desc";
  517. break;
  518. case 'price_asc':
  519. $order = "coin_price asc";
  520. break;
  521. default:
  522. $this->error('排序方式有误');
  523. break;
  524. }
  525. $whereCategory = [];
  526. if (!empty($category_id)) {
  527. $whereCategory = ['category_id' => $category_id];
  528. }
  529. $whereSearch = [];
  530. if (!empty($search)) {
  531. // 过滤非法字符
  532. $search = preg_replace("/[!@#$%^&*=\-+_\[\]{};':\"\/<>?]+/", '', trim($search));
  533. if (!empty($search)) {
  534. // 创建搜索记录
  535. if ($this->auth->isLogin()) {
  536. if (!SearchHistory::where(['user_id' => $this->auth->id, 'search' => $search])->value('id')) {
  537. SearchHistory::create(['user_id' => $this->auth->id, 'search' => $search]);
  538. }
  539. }
  540. // 构建商品ID搜索
  541. $condition_goods_ids = Goods::field('id')->where('status', 'online')->where("goods_name like '%{$search}%'")->buildSql();
  542. // 构建商品、盲盒绑定
  543. $condition_box_detail = Detail::field('box_id')->where("goods_id in {$condition_goods_ids}")->buildSql();
  544. $whereSearch = "box_name like '%{$search}%' or id in $condition_box_detail";
  545. }
  546. }
  547. // 查询空盲盒
  548. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  549. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  550. $list = Box::alias('box')
  551. ->field('id box_id,box_name,coin_price,box_banner_images')
  552. ->whereNotIn('id', $emptyBoxIds)
  553. ->where($wherePrice)
  554. ->where($whereCategory)
  555. ->where($whereSearch)
  556. ->where(['switch'=>1])
  557. ->order($order)
  558. ->paginate($pagesize, false, ['page' => $page])
  559. ->each(function ($item) {
  560. // 查询前6个商品图片
  561. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  562. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  563. foreach ($goods_images as &$image) {
  564. $image = cdnurl($image, true);
  565. }
  566. // 查询最低价
  567. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  568. $item->goods_num = count($allGoods);
  569. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  570. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  571. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  572. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  573. $item->goods_images = $goods_images;
  574. });
  575. $this->success('查询成功', $list);
  576. }
  577. /**
  578. * 盲盒详情
  579. * @throws \think\db\exception\DataNotFoundException
  580. * @throws \think\db\exception\ModelNotFoundException
  581. * @throws \think\exception\DbException
  582. * @author fuyelk <fuyelk@fuyelk.com>
  583. */
  584. public function boxDetail()
  585. {
  586. $box_id = input('box_id/d');
  587. if (empty($box_id)) {
  588. $this->error('请选择盲盒');
  589. }
  590. // 查询金币余额
  591. $mycoin = $this->auth->isLogin() ? $this->auth->coin : 0;
  592. // 查询是否收藏
  593. $is_star = $this->auth->isLogin() ? Star::check($this->auth->id, $box_id) : 0;
  594. // 查询盲盒基础信息
  595. $box = Box::field('box_banner_images,box_banner_images_desc,box_name,coin_price,box_banner_images')->where('id', $box_id)->find();
  596. if (empty($box)) {
  597. $this->error('盲盒有误');
  598. }
  599. $tagName = [
  600. 'normal' => '普通',
  601. 'rare' => '稀有',
  602. 'supreme' => '史诗',
  603. 'legend' => '传说',
  604. ];
  605. // 查询商品id及概率
  606. $detail = Detail::where('box_id', $box_id)->order('weigh desc')->column('rate', 'goods_id');
  607. // // 查询前6个商品
  608. // $firstGoods = Goods::field('image,coin_price,goods_name,tag')
  609. // ->where('status', 'online')
  610. // ->whereIn('id', array_slice(array_keys($detail), 0, 1000))
  611. // ->select();
  612. $firstGoods = Detail::alias("a")->join("goods b","b.id = a.goods_id")->where('a.box_id', $box_id)->field("b.image,b.xiangqing,b.coin_price,b.goods_name,b.tag")->order("a.weigh desc")->select();
  613. foreach ($firstGoods as &$first) {
  614. $first->image = $first->image ? cdnurl($first->image, true) : $first->image;
  615. $first->tag = $tagName[$first->tag];
  616. // $first->price = round(Setting::getRmbFromCoin($first->coin_price ?: 0), 2);
  617. $first->price = $first->coin_price;
  618. if (!empty($first->xiangqing)) {
  619. if (strstr($first['xiangqing'],',')) {
  620. $first['xiangqing'] = explode(',',$first['xiangqing']);
  621. // var_dump($first);
  622. // exit();
  623. }
  624. }
  625. $first->hidden(['coin_price']);
  626. }
  627. // var_dump($first);exit();
  628. // 查询全部商品
  629. $moreGoods = Goods::field('id,image,xiangqing,coin_price,goods_name,tag')
  630. ->where('status', 'online')
  631. ->whereIn('id', array_keys($detail))
  632. ->select();
  633. // 整理商品信息并记录每个类别的概率总合
  634. $rateList = [];
  635. foreach ($moreGoods as &$more) {
  636. if (isset($rateList[$more->tag])) {
  637. $rateList[$more->tag] += $detail[$more->id];
  638. } else {
  639. $rateList[$more->tag] = $detail[$more->id];
  640. }
  641. $more->image = $more->image ? cdnurl($more->image, true) : $more->image;
  642. $more->tag = $tagName[$more->tag];
  643. $more->price = round(Setting::getRmbFromCoin($more->coin_price ?: 0), 2);
  644. if (strstr($more->xiangqing,',')) {
  645. $more->xiangqing = explode(',',$more->xiangqing);
  646. }
  647. $more->hidden(['id,coin_price']);
  648. }
  649. $tags = [
  650. 'normal' => 0,
  651. 'rare' => 0,
  652. 'supreme' => 0,
  653. 'legend' => 0
  654. ];
  655. // 没有的商品概率设为0
  656. foreach ($tags as $tag => &$rate) {
  657. if (isset($rateList[$tag])) {
  658. $rate = $rateList[$tag];
  659. }
  660. }
  661. // 计算全部类别概率总和
  662. $rate_sum = array_sum(array_values($tags));
  663. // 计算每个类别概率
  664. foreach ($tags as $tag => &$rate) {
  665. $rate = $rate_sum ? (round($rate / $rate_sum, 4) * 100) : 0 . '%';
  666. }
  667. // 查询该盲盒开箱记录
  668. $prize = Prizerecord::alias('prize')
  669. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  670. ->field('user.nickname,user.avatar')
  671. ->join('user user', 'user.id = prize.user_id')
  672. ->where('prize.box_id', $box_id)
  673. ->order('prize.id', 'desc')
  674. ->limit(10)
  675. ->select();
  676. foreach ($prize as $prize_item) {
  677. $prize_item->create_time = date('Y-m-d H:i:s', $prize_item->create_time);
  678. $prize_item->avatar = $prize_item->avatar ? cdnurl($prize_item->avatar, true) : letter_avatar($prize_item->nickname);
  679. $prize_item->goods_image = $prize_item->goods_image ? cdnurl($prize_item->goods_image, true) : '';
  680. }
  681. $box_banner_images = [];
  682. $box_banner = [];
  683. $box->box_banner_images = explode(',', $box->box_banner_images);
  684. $banner_desc = $box->box_banner_images_desc;
  685. $banner_desc = $banner_desc ? json_decode($banner_desc, true) : [];
  686. foreach ($box->box_banner_images as $index => $image) {
  687. $image = $image ? cdnurl($image, true) : '';
  688. $box_banner_images[] = $image;
  689. $box_banner[] = [
  690. 'desc' => $banner_desc[$index] ?? '',
  691. 'image' => $image
  692. ];
  693. }
  694. $ret = [
  695. 'mycoin' => $mycoin,
  696. 'is_star' => $is_star,
  697. 'box_banner_images' => $box_banner_images,
  698. 'box_banner' => $box_banner,
  699. 'box_name' => $box->box_name,
  700. 'coin_price' => intval($box->coin_price),
  701. 'goodslist' => $firstGoods,
  702. 'more' => [
  703. 'goodslist' => $moreGoods,
  704. 'tags' => $tags
  705. ],
  706. 'record' => $prize
  707. ];
  708. $this->success('查询成功', $ret);
  709. }
  710. /**
  711. * 查询更多盲盒开箱记录
  712. * @throws DbException
  713. * @author fuyelk <fuyelk@fuyelk.com>
  714. */
  715. public function boxOpenRecord()
  716. {
  717. $box_id = input('box_id/d');
  718. $pagesize = input('pagesize/d', 10);
  719. $page = input('page/d', 1);
  720. if (empty($box_id)) {
  721. $this->error('请选择盲盒');
  722. }
  723. // 查询该盲盒开箱记录
  724. $record = Prizerecord::alias('prize')
  725. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  726. ->field('user.nickname,user.avatar')
  727. ->join('user user', 'user.id = prize.user_id')
  728. ->where('prize.box_id', $box_id)
  729. ->order('prize.id', 'desc')
  730. ->paginate($pagesize, false, ['page' => $page])
  731. ->each(function ($item) {
  732. $item->create_time = date('Y-m-d H:i:s', $item->create_time);
  733. $item->avatar = $item->avatar ? cdnurl($item->avatar, true) : letter_avatar($item->nickname);
  734. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  735. });
  736. $this->success('查询成功', $record);
  737. }
  738. /**
  739. * 试一试盲盒详情
  740. * @throws \think\db\exception\DataNotFoundException
  741. * @throws \think\db\exception\ModelNotFoundException
  742. * @throws \think\exception\DbException
  743. * @author fuyelk <fuyelk@fuyelk.com>
  744. */
  745. public function tryBoxDetail()
  746. {
  747. // 查询试一试盲盒
  748. $box_id = Box::where('is_try', 1)->value('id');
  749. // 查询金币余额
  750. $mycoin = $this->auth->isLogin() ? $this->auth->coin : 0;
  751. // 查询是否收藏
  752. $is_star = $this->auth->isLogin() ? Star::check($this->auth->id, $box_id) : 0;
  753. // 查询盲盒基础信息
  754. $box = Box::field('box_banner_images,box_name,coin_price')->where('id', $box_id)->find();
  755. if (empty($box)) {
  756. $this->error('盲盒有误');
  757. }
  758. $tagName = [
  759. 'normal' => '普通',
  760. 'rare' => '稀有',
  761. 'supreme' => '史诗',
  762. 'legend' => '传说',
  763. ];
  764. // 查询商品id及概率
  765. $detail = Detail::where('box_id', $box_id)->order('weigh', 'desc')->column('rate', 'goods_id');
  766. // 查询前6个商品
  767. $firstGoods = Goods::field('image,coin_price,goods_name,tag')
  768. ->where('status', 'online')
  769. ->whereIn('id', array_slice(array_keys($detail), 0, 6))
  770. ->select();
  771. foreach ($firstGoods as &$first) {
  772. $first->image = $first->image ? cdnurl($first->image, true) : $first->image;
  773. $first->tag = $tagName[$first->tag];
  774. $first->price = round(Setting::getRmbFromCoin($first->coin_price ?: 0), 2);
  775. $first->hidden(['coin_price']);
  776. }
  777. // 查询全部商品
  778. $moreGoods = Goods::field('id,image,coin_price,goods_name,tag')
  779. ->where('status', 'online')
  780. ->whereIn('id', array_keys($detail))
  781. ->select();
  782. // 整理商品信息并记录每个类别的概率总合
  783. $rateList = [];
  784. foreach ($moreGoods as &$more) {
  785. if (isset($rateList[$more->tag])) {
  786. $rateList[$more->tag] += $detail[$more->id];
  787. } else {
  788. $rateList[$more->tag] = $detail[$more->id];
  789. }
  790. $more->image = $more->image ? cdnurl($more->image, true) : $more->image;
  791. $more->tag = $tagName[$more->tag];
  792. $more->price = round(Setting::getRmbFromCoin($more->coin_price ?: 0), 2);
  793. $more->hidden(['id,coin_price']);
  794. }
  795. $tags = [
  796. 'normal' => 0,
  797. 'rare' => 0,
  798. 'supreme' => 0,
  799. 'legend' => 0
  800. ];
  801. // 没有的商品概率设为0
  802. foreach ($tags as $tag => &$rate) {
  803. if (isset($rateList[$tag])) {
  804. $rate = $rateList[$tag];
  805. }
  806. }
  807. // 计算全部类别概率总和
  808. $rate_sum = array_sum(array_values($tags));
  809. // 计算每个类别概率
  810. foreach ($tags as $tag => &$rate) {
  811. $rate = round($rate / $rate_sum, 4) * 100 . '%';
  812. }
  813. // 查询该盲盒开箱记录
  814. $prize = Prizerecord::alias('prize')
  815. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  816. ->field('user.nickname,user.avatar')
  817. ->join('user user', 'user.id = prize.user_id')
  818. ->where('prize.box_id', $box_id)
  819. ->order('prize.id', 'desc')
  820. ->limit(3)
  821. ->select();
  822. foreach ($prize as $prize_item) {
  823. $prize_item->create_time = date('Y-m-d H:i:s', $prize_item->create_time);
  824. $prize_item->avatar = $prize_item->avatar ? cdnurl($prize_item->avatar, true) : letter_avatar($prize_item->nickname);
  825. $prize_item->goods_image = $prize_item->goods_image ? cdnurl($prize_item->goods_image, true) : '';
  826. }
  827. $box_banner_images = [];
  828. $box->box_banner_images = explode(',', $box->box_banner_images);
  829. foreach ($box->box_banner_images as $box_banner_image) {
  830. $box_banner_images[] = cdnurl($box_banner_image, true);
  831. }
  832. $ret = [
  833. 'box_id' => $box_id,
  834. 'mycoin' => $mycoin,
  835. 'is_star' => $is_star,
  836. 'box_banner_images' => $box_banner_images,
  837. 'box_name' => $box->box_name,
  838. 'coin_price' => intval($box->coin_price),
  839. 'goodslist' => $firstGoods,
  840. 'more' => [
  841. 'goodslist' => $moreGoods,
  842. 'tags' => $tags
  843. ],
  844. 'record' => $prize
  845. ];
  846. $this->success('查询成功', $ret);
  847. }
  848. /**
  849. * 抽奖测试
  850. * @author fuyelk <fuyelk@fuyelk.com>
  851. */
  852. public function test()
  853. {
  854. $prize = [];
  855. $times = $index = 1000;
  856. while (--$index >= 0) {
  857. $p = Detail::getOne(1);
  858. if (isset($prize[$p])) {
  859. $prize[$p]++;
  860. } else {
  861. $prize[$p] = 1;
  862. }
  863. }
  864. foreach ($prize as $goods => &$rate) {
  865. $rate = round($rate / $times, 2) * 100;
  866. }
  867. arsort($prize);
  868. de($prize);
  869. }
  870. /**
  871. * 点赞
  872. * @author fuyelk <fuyelk@fuyelk.com>
  873. */
  874. public function star()
  875. {
  876. $box_id = input('box_id/d');
  877. try {
  878. $res = Star::click($this->auth->id, $box_id);
  879. } catch (Exception $e) {
  880. $this->error('收藏失败');
  881. }
  882. if ($res) {
  883. $this->success('收藏成功', ['is_star' => 1]);
  884. }
  885. $this->success('取消收藏成功', ['is_star' => 0]);
  886. }
  887. /**
  888. * 试玩
  889. * @throws DataNotFoundException
  890. * @throws DbException
  891. * @throws ModelNotFoundException
  892. * @author fuyelk <fuyelk@fuyelk.com>
  893. */
  894. public function haveATry()
  895. {
  896. $box_id = input('box_id/d');
  897. $num = input('num/d');
  898. $select = input('select', '随机');
  899. if (empty($box_id)) {
  900. $this->error('请选择盲盒');
  901. }
  902. if (!in_array($num, [1, 5])) {
  903. $this->error('开箱数量有误');
  904. }
  905. // 检查盲盒是否可以试玩
  906. // $box = Box::where('id', $box_id)->where('is_try', 1)->value('id');
  907. // if (empty($box)) {
  908. // $this->error('该盲盒暂时不可试玩');
  909. // }
  910. try {
  911. if (1 == $num) {
  912. $goodsIds = [Detail::getOnes($box_id)];
  913. } else {
  914. $goodsIds = Detail::getMores($box_id, $num);
  915. }
  916. } catch (\Exception $e) {
  917. $this->error($e->getMessage());
  918. }
  919. foreach ($goodsIds as $goodsId) {
  920. $goodsInfo[] = Goods::field('image,goods_name,tag')->where('id', $goodsId)->find();
  921. }
  922. if (empty($goodsInfo)) {
  923. $this->error('抽奖失败');
  924. }
  925. foreach ($goodsInfo as $item) {
  926. if ($item->image) {
  927. $item->image = cdnurl($item->image, true);
  928. }
  929. }
  930. $ret = [
  931. 'select' => $select,
  932. 'goodsInfo' => $goodsInfo
  933. ];
  934. $this->success('抽奖成功', $ret);
  935. }
  936. /**
  937. * 创建盲盒订单
  938. * @author fuyelk <fuyelk@fuyelk.com>
  939. */
  940. public function createOrder()
  941. {
  942. $box_id = input('box_id/d');
  943. $num = input('num/d');
  944. $select = input('select', '');
  945. if (empty($box_id)) {
  946. $this->error('未选择盲盒');
  947. }
  948. if (!in_array($num, [1, 5, 9])) {
  949. $this->error('选择的盲盒数量有误');
  950. }
  951. // 检查盲盒
  952. $box = Box::field('id,box_name,box_banner_images,coin_price,box_banner_images')->where('id', $box_id)->find();
  953. if (empty($box)) {
  954. $this->error('选择的盲盒有误');
  955. }
  956. // 检查盲盒奖品
  957. $prize = Detail::where('box_id', $box_id)->order('weigh desc,id asc')->value('goods_id');
  958. if (empty($prize)) {
  959. $this->error('这个盲盒太火爆吧,商品暂时缺货!');
  960. }
  961. // 查询前6个商品的图片
  962. $goodsIds = Detail::where('box_id', $box_id)->order('weigh desc,id asc')->column('goods_id');
  963. $goodsImages = Goods::where('status', 'online')
  964. ->whereIn('id', $goodsIds)
  965. ->column('image');
  966. foreach ($goodsImages as &$image) {
  967. $image = cdnurl($image, true);
  968. }
  969. Db::startTrans();
  970. try {
  971. $rmb_price = round(Setting::getRmbFromCoin($box->coin_price ?: 0), 2);
  972. $res = Order::create([
  973. 'box_id' => $box_id,
  974. 'box_name' => $box->box_name,
  975. 'image' => $goodsImages[0] ?? '',
  976. 'coin_price' => $box->coin_price,
  977. 'rmb_price' => $rmb_price,
  978. 'num' => $num,
  979. 'coin_amount' => $box->coin_price * $num,
  980. 'rmb_amount' => $rmb_price * $num,
  981. 'user_id' => $this->auth->id,
  982. 'select' => $select,
  983. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  984. ]);
  985. $ret = [
  986. 'order_id' => intval($res->id),
  987. 'box_name' => $res->box_name,
  988. 'images' => $goodsImages,
  989. 'coin_amount' => $res->coin_amount,
  990. 'rmb_amount' => $res->rmb_amount,
  991. 'notice' => Text::getText('pay_tipstips'),
  992. 'coin_not_enough' => !!(intval($this->auth->coin) < intval($res->coin_amount)),
  993. 'alipay' => $this->request->domain() . '/api/alipay/boxpay/orderid/' . intval($res->id),
  994. 'wechat' => '/api/wechat/boxpay/orderid/' . intval($res->id),
  995. ];
  996. } catch (Exception $e) {
  997. Db::rollback();
  998. $this->error('创建订单失败');
  999. }
  1000. Db::commit();
  1001. $this->success('创建订单成功', $ret);
  1002. }
  1003. /**
  1004. * 创建充值订单
  1005. * @author fuyelk <fuyelk@fuyelk.com>
  1006. */
  1007. public function createRechargeOrder()
  1008. {
  1009. $amount = input('amount/d');
  1010. if (empty($amount)) {
  1011. $this->error('金额不能为空');
  1012. }
  1013. // 检查金额是否允许
  1014. $rmb = RechargeList::where('coin', intval($amount))->value('rmb');
  1015. if (empty($rmb)) {
  1016. $this->error('充值金额有误');
  1017. }
  1018. Db::startTrans();
  1019. try {
  1020. $res = RechargeOrder::create([
  1021. 'user_id' => $this->auth->id,
  1022. 'coin_amount' => intval($amount),
  1023. 'rmb_amount' => round($rmb, 2),
  1024. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  1025. ]);
  1026. } catch (\Exception $e) {
  1027. Db::rollback();
  1028. $this->error('创建订单出错');
  1029. }
  1030. Db::commit();
  1031. $ret = [
  1032. 'alipay' => $this->request->domain() . '/api/alipay/rechargepay/orderid/' . intval($res->id),
  1033. 'wechat' => '/api/wechat/rechargepay/orderid/' . intval($res->id),
  1034. 'eepay' => '/api/eepay/rechargepay/orderid/' . intval($res->id),
  1035. ];
  1036. $this->success('创建订单成功', $ret);
  1037. }
  1038. /**
  1039. * 创建充值订单
  1040. * @author fuyelk <fuyelk@fuyelk.com>
  1041. */
  1042. public function createRechargeOrder_bak()
  1043. {
  1044. $amount = input('amount/d');
  1045. if (empty($amount)) {
  1046. $this->error('金额不能为空');
  1047. }
  1048. // 检查金额是否允许
  1049. $rmb = RechargeList::where('coin', intval($amount))->value('rmb');
  1050. if (empty($rmb)) {
  1051. $this->error('充值金额有误');
  1052. }
  1053. Db::startTrans();
  1054. try {
  1055. $res = RechargeOrder::create([
  1056. 'user_id' => $this->auth->id,
  1057. 'coin_amount' => intval($amount),
  1058. 'rmb_amount' => round($rmb, 2),
  1059. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  1060. ]);
  1061. } catch (\Exception $e) {
  1062. Db::rollback();
  1063. $this->error('创建订单出错');
  1064. }
  1065. Db::commit();
  1066. $ret = [
  1067. 'alipay' => $this->request->domain() . '/api/alipay/rechargepay/orderid/' . intval($res->id),
  1068. 'wechat' => '/api/wechat/rechargepay/orderid/' . intval($res->id),
  1069. ];
  1070. $this->success('创建订单成功', $ret);
  1071. }
  1072. /**
  1073. * 金币支付
  1074. * @author fuyelk <fuyelk@fuyelk.com>
  1075. */
  1076. public function coinPay()
  1077. {
  1078. $order_id = input('order_id/d');
  1079. if (empty($order_id)) {
  1080. $this->error('请选择支付订单');
  1081. }
  1082. $order = Order::field('id,box_id,num,status,coin_amount,out_trade_no,select')
  1083. ->where('id', $order_id)
  1084. ->where('user_id', $this->auth->id)
  1085. ->find();
  1086. if (empty($order)) {
  1087. $this->error('订单不存在');
  1088. }
  1089. if ('unpay' != $order->status) {
  1090. $this->error('该订单已支付,请勿重复支付');
  1091. }
  1092. // 查询用户余额
  1093. if (intval($this->auth->coin) < $order->coin_amount) {
  1094. $this->error('您的金币不足');
  1095. }
  1096. Db::startTrans();
  1097. try {
  1098. // 更新订单信息
  1099. $order->pay_method = 'coin';
  1100. $order->pay_coin = $order->coin_amount;
  1101. $order->pay_time = time();
  1102. $order->status = 'unused';// 状态:unpay=待支付,unused=待抽奖,used=已使用
  1103. $order->backend_read = 0;
  1104. $order->save();
  1105. $coin_before = $this->auth->coin;
  1106. // 减少金币余额
  1107. $user = $this->auth->getUser();
  1108. $user->setDec('coin', $order->pay_coin);
  1109. // 创建金币使用记录
  1110. CoinRecord::create([
  1111. 'user_id' => $this->auth->id,
  1112. 'before' => $coin_before,
  1113. 'after' => $this->auth->coin,
  1114. 'coin' => -$order->pay_coin,
  1115. 'order_id' => $order->id,
  1116. 'type' => 'pay_box', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入
  1117. ]);
  1118. $users= Db::table('box_user') ->where('id',$this->auth->pid)->find();
  1119. $orderadd = Order::where('id', $order_id)->where('user_id', $this->auth->id)->find();
  1120. $lou = Db::table('box_setting')->where('id', 1)->find();
  1121. $kou = $lou['kou'];
  1122. if($users['recharnum'] == 1){
  1123. Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1124. Retail::giveMoneys($orderadd);
  1125. }else{
  1126. Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1127. $userarr= Db::table('box_user') ->where('id',$users['id'])->find();
  1128. if(bcmod($userarr['recharnum'],$kou) == 0){
  1129. }else{
  1130. Retail::giveMoneys($orderadd);
  1131. };
  1132. }
  1133. // print_r(bcmod($users['recharnum'],$kou);
  1134. } catch (\Exception $e) {
  1135. Db::rollback();
  1136. dta('用户支付失败,订单已回滚到待支付');
  1137. $this->error('支付失败');
  1138. }
  1139. Db::commit();
  1140. // 开箱
  1141. $prize = $this->open($order);
  1142. $this->success('支付成功', ['prize' => $prize]);
  1143. }
  1144. /**
  1145. * 余额支付
  1146. * @author fuyelk <fuyelk@fuyelk.com>
  1147. */
  1148. public function cmoneyPay()
  1149. {
  1150. $order_id = input('order_id/d');
  1151. if (empty($order_id)) {
  1152. $this->error('请选择支付订单');
  1153. }
  1154. $order = Order::field('id,box_id,num,status,rmb_amount,out_trade_no,select')
  1155. ->where('id', $order_id)
  1156. ->where('user_id', $this->auth->id)
  1157. ->find();
  1158. if (empty($order)) {
  1159. $this->error('订单不存在');
  1160. }
  1161. if ('unpay' != $order->status) {
  1162. $this->error('该订单已支付,请勿重复支付');
  1163. }
  1164. // 查询用户余额
  1165. if (intval($this->auth->money) < $order->rmb_amount) {
  1166. $this->error('您的余额不足');
  1167. }
  1168. Db::startTrans();
  1169. try {
  1170. // 更新订单信息
  1171. $order->pay_method = 'money';
  1172. $order->rmb_amount = $order->rmb_amount;
  1173. $order->pay_time = time();
  1174. $order->status = 'unused';// 状态:unpay=待支付,unused=待抽奖,used=已使用
  1175. $order->backend_read = 0;
  1176. $order->save();
  1177. $coin_before = $this->auth->money;
  1178. // 减少余额
  1179. $user = $this->auth->getUser();
  1180. $user->setDec('money', $order->rmb_amount);
  1181. // 创建余额使用记录
  1182. MoneyRecord::create([
  1183. 'user_id' => $this->auth->id,
  1184. 'before' => $this->auth->money,
  1185. 'after' => $this->auth->money-$order->rmb_amount,
  1186. 'money' => -$order->rmb_amount,
  1187. 'order_id' => $order->id,
  1188. 'type' => 'yezhifu', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败,yezhifu=余额支付
  1189. ]);
  1190. // $users= Db::table('box_user') ->where('id',$this->auth->pid)->find();
  1191. // $orderadd = Order::where('id', $order_id)->where('user_id', $this->auth->id)->find();
  1192. // $lou = Db::table('box_setting')->where('id', 1)->find();
  1193. // $kou = $lou['kou'];
  1194. // if($users['recharnum'] == 1){
  1195. // Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1196. // Retail::giveMoneys($orderadd);
  1197. // }else{
  1198. // Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1199. // $userarr= Db::table('box_user') ->where('id',$users['id'])->find();
  1200. // // if(bcmod($userarr['recharnum'],$kou) == 0){
  1201. // // }else{
  1202. // // Retail::giveMoneys($orderadd);
  1203. // // };
  1204. // }
  1205. // print_r(bcmod($users['recharnum'],$kou);
  1206. } catch (\Exception $e) {
  1207. Db::rollback();
  1208. dta('用户支付失败,订单已回滚到待支付');
  1209. $this->error('支付失败');
  1210. }
  1211. Db::commit();
  1212. // 开箱
  1213. $prize = $this->open($order);
  1214. $this->success('支付成功', ['prize' => $prize]);
  1215. }
  1216. /**
  1217. * 价格区间
  1218. * @author fuyelk <fuyelk@fuyelk.com>
  1219. */
  1220. public function priceRange()
  1221. {
  1222. $list = PriceRange::order('weigh desc,id asc')->column('range');
  1223. $this->success('查询成功', ['range' => $list]);
  1224. }
  1225. /**
  1226. * 充值列表
  1227. * @author fuyelk <fuyelk@fuyelk.com>
  1228. */
  1229. public function rechargeList()
  1230. {
  1231. $coinList = RechargeList::order('weigh desc,id desc')->column('coin,rmb', 'id');
  1232. $list = [];
  1233. foreach ($coinList as $item) {
  1234. $list[] = [
  1235. 'coin' => $item['coin'],
  1236. 'rmb' => floatval($item['rmb'])
  1237. ];
  1238. }
  1239. $num = Setting::getSetting('one_rmb_to_coin_num');
  1240. $ret = [
  1241. 'tips' => '充值金额数值比例1:' . round(Setting::getSetting('one_rmb_to_coin_num'), 2),
  1242. 'list' => $list,
  1243. 'bili' => $num,
  1244. 'notice' => Text::getText('recharge_introduction')
  1245. ];
  1246. $this->success('查询成功', $ret);
  1247. }
  1248. /**
  1249. * 开箱
  1250. * @param Order $order 订单
  1251. * @return array|bool
  1252. * @author fuyelk <fuyelk@fuyelk.com>
  1253. */
  1254. private function open(Order $order)
  1255. {
  1256. // 检查订单状态
  1257. if ('unused' != $order->status) { // 状态:unpay=待支付,unused=待抽奖,used=已使用
  1258. return false;
  1259. }
  1260. if (empty($order->box_id)) {
  1261. $this->error('请选择盲盒');
  1262. }
  1263. if (!in_array($order->num, [1, 5, 9])) {
  1264. $this->error('开箱数量有误');
  1265. }
  1266. // 检查盲盒是否可以试玩
  1267. $box = Box::where('id', $order->box_id)->value('id');
  1268. if (empty($box)) {
  1269. $this->error('盲盒有误');
  1270. }
  1271. try {
  1272. // 抽奖 begin
  1273. if (1 == $order->num) {
  1274. $goodsIds = [Detail::getOne($order->box_id)];
  1275. } else {
  1276. $goodsIds = Detail::getMore($order->box_id, $order->num);
  1277. }
  1278. } catch (\Exception $e) {
  1279. // 退款
  1280. if (!$this->refund($order)) {
  1281. $logID = "";
  1282. try {
  1283. $logID = dta($order->toArray(), '用户退款失败');
  1284. } catch (Exception $e) {
  1285. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1286. }
  1287. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1288. }
  1289. $this->error('抽奖失败,已退款');
  1290. }
  1291. // 抽奖 end
  1292. // 查询抽中的奖品信息
  1293. foreach ($goodsIds as $goodsId) {
  1294. $goodsInfo[] = Goods::field('id,image,tag,coin_price,goods_name,delivery_fee')->where('id', $goodsId)->find();
  1295. }
  1296. // 抽奖失败
  1297. if (empty($goodsInfo)) {
  1298. // 退款
  1299. if (!$this->refund($order)) {
  1300. $logID = "";
  1301. try {
  1302. $logID = dta($order->toArray(), '用户退款失败');
  1303. } catch (Exception $e) {
  1304. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1305. }
  1306. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1307. }
  1308. $this->error('抽奖失败,已退款');
  1309. }
  1310. $prizeInfo = [];
  1311. Db::startTrans();
  1312. try {
  1313. foreach ($goodsInfo as &$goods) {
  1314. // 创建开箱记录
  1315. $prize = Prizerecord::create([
  1316. 'box_id' => $order->box_id,
  1317. 'order_id' => $order->id,
  1318. 'out_trade_no' => $order->out_trade_no,
  1319. 'user_id' => $this->auth->id,
  1320. 'goods_id' => $goods->id,
  1321. 'goods_name' => $goods->goods_name,
  1322. 'goods_image' => $goods->image,
  1323. 'goods_coin_price' => $goods->coin_price,
  1324. 'goods_rmb_price' => round(Setting::getRmbFromCoin($goods->coin_price ?: 0), 2),
  1325. 'delivery_fee' => round($goods->delivery_fee, 2),
  1326. 'status' => 'bag', // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  1327. ]);
  1328. // 减少商品库存
  1329. Goods::where('id', $goods->id)->setDec('stock');
  1330. $prizeInfo[] = [
  1331. 'prize_id' => intval($prize->id),
  1332. 'image' => $prize->goods_image ? cdnurl($prize->goods_image, true) : '',
  1333. 'goods_name' => $prize->goods_name,
  1334. 'tag' => $goods->tag
  1335. ];
  1336. }
  1337. // 增加盲盒销量
  1338. Box::where('id', $order->box_id)->setInc('sales', $order->num);
  1339. } catch (\Exception $e) {
  1340. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '保存开箱记录、减少商品库存失败');
  1341. Db::rollback();
  1342. // 退款
  1343. if (!$this->refund($order)) {
  1344. $logID = "";
  1345. try {
  1346. $logID = dta($order->toArray(), '用户退款到金币失败');
  1347. } catch (Exception $e) {
  1348. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1349. }
  1350. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1351. }
  1352. $this->error('抽奖失败,已退款');
  1353. }
  1354. Db::commit();
  1355. // 订单状态改为已使用
  1356. $order->save(['status' => 'used', 'backend_read' => 0]);
  1357. $ret = [
  1358. 'select' => $order->select,
  1359. 'prizeInfo' => $prizeInfo
  1360. ];
  1361. return $ret;
  1362. }
  1363. /**
  1364. * 通过订单号打开盲盒
  1365. * @author fuyelk <fuyelk@fuyelk.com>
  1366. */
  1367. public function openByOrderTrade()
  1368. {
  1369. $out_trade_no = input('out_trade_no');
  1370. $order_id = input('order_id');
  1371. if (empty($out_trade_no) && empty($order_id)) {
  1372. $this->error('参数有误');
  1373. }
  1374. $where = [];
  1375. if (!empty($out_trade_no)) {
  1376. $where['out_trade_no'] = $out_trade_no;
  1377. }
  1378. if (!empty($order_id)) {
  1379. $where['id'] = $order_id;
  1380. }
  1381. $order = Order::where($where)->where('user_id', $this->auth->id)->where('status', 'unused')->find();
  1382. if (empty($order)) {
  1383. $this->error('订单有误');
  1384. }
  1385. // 开箱
  1386. $prize = $this->open($order);
  1387. $this->success('开箱成功', ['prize' => $prize]);
  1388. }
  1389. /**
  1390. * 退款自动选择
  1391. * @param Order $order
  1392. * @return bool
  1393. * @author fuyelk <fuyelk@fuyelk.com>
  1394. */
  1395. private function refund(Order $order)
  1396. {
  1397. switch ($order->pay_method) {
  1398. case 'coin' :
  1399. return $this->refundToCoin($order);
  1400. break;
  1401. case 'wechat':
  1402. return $this->refundToMoney($order);
  1403. break;
  1404. case 'alipay':
  1405. return $this->refundToMoney($order);
  1406. break;
  1407. default:
  1408. return false;
  1409. }
  1410. }
  1411. /**
  1412. * 退款到金币
  1413. * @param Order $order 订单
  1414. * @author fuyelk <fuyelk@fuyelk.com>
  1415. */
  1416. private function refundToCoin(Order $order)
  1417. {
  1418. $coin_before = $this->auth->coin;
  1419. Db::startTrans();
  1420. try {
  1421. // 增加金币余额
  1422. $user = $this->auth->getUser();
  1423. $user->setInc('coin', $order->pay_coin);
  1424. // 创建金币记录
  1425. CoinRecord::create([
  1426. 'user_id' => $this->auth->id,
  1427. 'before' => $coin_before,
  1428. 'after' => $this->auth->coin,
  1429. 'coin' => $order->pay_coin,
  1430. 'order_id' => $order->id,
  1431. 'type' => 'refund', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,refund=退款
  1432. ]);
  1433. // 更新订单状态为已退款
  1434. $order->save(['status' => 'refund', 'backend_read' => 0]);
  1435. } catch (\Exception $e) {
  1436. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '用户退款到金币失败');
  1437. Db::rollback();
  1438. return false;
  1439. }
  1440. Db::commit();
  1441. return true;
  1442. }
  1443. /**
  1444. * 退款到余额
  1445. * @param Order $order 订单
  1446. * @author fuyelk <fuyelk@fuyelk.com>
  1447. */
  1448. private function refundToMoney(Order $order)
  1449. {
  1450. $money_before = $this->auth->money;
  1451. Db::startTrans();
  1452. try {
  1453. // 增加金币余额
  1454. $user = $this->auth->getUser();
  1455. $user->setInc('money', $order->pay_rmb);
  1456. // 创建余额记录
  1457. MoneyRecord::create([
  1458. 'user_id' => $this->auth->id,
  1459. 'before' => $money_before,
  1460. 'after' => $this->auth->money,
  1461. 'money' => $order->pay_rmb,
  1462. 'order_id' => $order->id,
  1463. 'type' => 'refund', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  1464. ]);
  1465. // 更新订单状态为已退款
  1466. $order->save(['status' => 'refund', 'backend_read' => 0]);
  1467. } catch (\Exception $e) {
  1468. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '用户退款到余额失败');
  1469. Db::rollback();
  1470. return false;
  1471. }
  1472. Db::commit();
  1473. return true;
  1474. }
  1475. /**
  1476. * 获取客服信息
  1477. * @author fuyelk <fuyelk@fuyelk.com>
  1478. */
  1479. public function getServiceInfo()
  1480. {
  1481. $ret = [
  1482. 'number' => Setting::getSetting('service_number') ?: '',
  1483. 'qrcode' => ($qrcode = Setting::getSetting('service_qrcode')) ? cdnurl($qrcode, true) : '',
  1484. ];
  1485. $this->success('查询成功', $ret);
  1486. }
  1487. /**
  1488. * 获取落地域名
  1489. * @author fuyelk <fuyelk@fuyelk.com>
  1490. */
  1491. public function getServiceurl()
  1492. {
  1493. $ret = Setting::where('id',1)->find();
  1494. // $curl = Setting::getSetting('curl');
  1495. // $abs = explode('|',$curl);
  1496. // $ret = [
  1497. // 'url' => Setting::getSetting('url')
  1498. // ];
  1499. $this->success('查询成功', $ret['url']);
  1500. }
  1501. /**
  1502. * 获取微信登录地址
  1503. * @author fuyelk <fuyelk@fuyelk.com>
  1504. */
  1505. public function getWechatLoginUrl()
  1506. {
  1507. $redirect = input('redirect');
  1508. if (empty($redirect)) {
  1509. $this->error('重定向地址不能为空');
  1510. }
  1511. $redirect = preg_match("/^http(.*)/", $redirect) ? $redirect : $this->request->domain() . $redirect;
  1512. $ret = [
  1513. 'url' => $this->request->domain() . '/index/wechat/bootToUrl?url=' . base64_encode($redirect)
  1514. ];
  1515. $this->success('查询成功', $ret);
  1516. }
  1517. /**
  1518. * 添加晒图
  1519. */
  1520. public function setShai(){
  1521. $validate = new \think\Validate([
  1522. 'box_zw' => 'require',
  1523. 'box_img' => 'require',
  1524. 'token' => 'require',
  1525. ]);
  1526. $validate->message([
  1527. 'box_zw.require' => '盲盒详情banner文字不能为空',
  1528. 'box_img.require' => '盲盒详情banner不能为空',
  1529. 'token.require' => 'token不能为空',
  1530. ]);
  1531. $data = $this->request->param();
  1532. if (!$validate->check($data)) {
  1533. $this->error($validate->getError());
  1534. }
  1535. $params = [
  1536. 'box_mc'=>$this->auth->nickname,
  1537. 'box_tx'=>$this->auth->avatar ? cdnurl($this->auth->avatar, true) : '',
  1538. 'box_zw'=>$data['box_zw'],
  1539. 'box_img'=>$data['box_img'],
  1540. 'create_time'=>time()
  1541. ];
  1542. Db::table('box_shai')->insertGetId($params);
  1543. $this->success();
  1544. }
  1545. }