Attire.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 装扮商城接口
  8. */
  9. class Attire extends Api
  10. {
  11. protected $noNeedLogin = ['getAttireList'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->attireModel = new \app\common\model\Attire();
  17. $this->attirebackModel = new \app\common\model\AttireBack();
  18. }
  19. /**
  20. * 获取装扮列表
  21. */
  22. public function getAttireList() {
  23. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  24. $page = $this->request->request('page',1); // 分页
  25. $pageNum = $this->request->request('pageNum',10); // 分页
  26. // 分页搜索构建
  27. $pageStart = ($page-1)*$pageNum;
  28. // 获取基本信息
  29. $where = [];
  30. $where["type"] = $type;
  31. $where["is_show"] = 1;
  32. $attireList = $this->attireModel->where($where)->limit($pageStart,$pageNum)->select();
  33. if($attireList) {
  34. foreach($attireList as $k => $v) {
  35. }
  36. }
  37. $this->success("获取成功!",$attireList);
  38. }
  39. /**
  40. * 购买并加入我的背包
  41. */
  42. public function attireAddToMyBack() {
  43. $attire_id = $this->request->request("attire_id"); //装扮ID
  44. if (!$attire_id) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. if ($this->auth->power->attire == 1) {
  48. $this->error('您已被禁止购买装扮');
  49. }
  50. // 判断用户钻石余额是否充足
  51. $userModel = new \app\common\model\User();
  52. $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
  53. $userwallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  54. if(!$userInfo) {
  55. $this->error("用户信息获取失败!");
  56. }
  57. // 获取购买装扮需要的价格
  58. $attireInfo = $this->attireModel->where(["id"=>$attire_id])->find();
  59. if(!$attireInfo) {
  60. $this->error("装扮信息获取失败!");
  61. }
  62. if($userwallet["jewel"] < $attireInfo["price"]) {
  63. $this->error("您的余额不足,请先充值!");
  64. }
  65. // 进行购买逻辑
  66. Db::startTrans();
  67. try{
  68. // 扣除用户钻石余额
  69. $where = [];
  70. $where["id"] = $this->auth->id;
  71. $res1 = true;
  72. // 添加当前用户钻石流水记录
  73. $res2 = true;
  74. //增加用户钻石余额
  75. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id, $attireInfo["price"], "-", 0, "购买装扮扣除" . $attireInfo["price"] . "钻石!", 6,'jewel');
  76. if($rs_wallet['status'] == false){
  77. Db::rollback();
  78. $this->error($rs_wallet['msg']);
  79. }
  80. // 添加装扮购买记录
  81. $data = [];
  82. $data["user_id"] = $this->auth->id;
  83. $data["attire_id"] = $attire_id;
  84. $data["price"] = $attireInfo["price"];
  85. $data["attire_name"] = $attireInfo["title"];
  86. $data["createtime"] = time();
  87. $attirebylogModel = new \app\common\model\AttireBuyLog();
  88. $res3 = $attirebylogModel->insertGetId($data);
  89. // 加入我的背包
  90. // $where = [];
  91. // $where["user_id"] = $this->auth->id;
  92. // $where["attire_id"] = $attire_id;
  93. // $attirebackInfo = $this->attirebackModel->where($where)->find();
  94. $data = [];
  95. $data["user_id"] = $this->auth->id;
  96. $data["attire_id"] = $attire_id;
  97. $data["attire_name"] = $attireInfo["title"];
  98. $data["price"] = $attireInfo["price"];
  99. $data["file_image"] = $attireInfo["file_image"];
  100. $data["android_image"] = $attireInfo["android_image"];
  101. $data["gif_image"] = $attireInfo["gif_image"];
  102. $data["limit_day"] = $attireInfo["limit_day"];
  103. $data["type"] = $attireInfo["type"];
  104. $data["createtime"] = time();
  105. // if($attirebackInfo) { // 背包中已有商品
  106. // if($attirebackInfo["is_use"] == 1) { // 商品正在使用中
  107. // if($attirebackInfo["duetime"] >= time()) { // 使用中未到期
  108. // $duetime = $attireInfo["limit_day"]*86400;
  109. // $res4 = $this->attirebackModel->setInc("duetime",$duetime);
  110. // } else { // 使用中已到期
  111. // $res4 = $this->attirebackModel->insert($data);
  112. // }
  113. // } else { // 商品未使用
  114. // $res4 = $this->attirebackModel->insert($data);
  115. // }
  116. // } else { // 背包中没有此商品
  117. // $res4 = $this->attirebackModel->insert($data);
  118. // }
  119. $res4 = $this->attirebackModel->insert($data);
  120. if($res1 && $res2 && $res3 && $res4) {
  121. Db::commit();
  122. $this->success("购买成功!");
  123. } else {
  124. $this->error("购买失败,请稍后重试!");
  125. }
  126. }catch (ValidateException $e) {
  127. Db::rollback();
  128. $this->error($e->getMessage());
  129. } catch (PDOException $e) {
  130. Db::rollback();
  131. $this->error($e->getMessage());
  132. } catch (Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. }
  137. /**
  138. * 获取我的背包装饰列表
  139. */
  140. public function getBackAttireList() {
  141. try {
  142. $page = $this->request->request('page',1); // 分页
  143. $pageNum = $this->request->request('pageNum',10); // 分页
  144. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  145. // 分页搜索构建
  146. $pageStart = ($page-1)*$pageNum;
  147. global $where;
  148. $where = [];$whereOr=[];
  149. $where["user_id"] = $this->auth->id;
  150. $type && $where["type"] = $type;
  151. $whereOr["is_use"] = "0";
  152. $whereOr["duetime"] = ["gt",time()];
  153. $list = $this->attirebackModel
  154. ->field("id,type,file_image,gif_image,attire_name as title,limit_day,duetime,is_use,is_using")
  155. ->where(function ($query) {
  156. global $where;
  157. $query->where($where);
  158. })
  159. ->where(function ($query) {
  160. $query->whereOr(["is_use"=>0,"duetime"=>["gt",time()]]);
  161. })
  162. ->limit($pageStart,$pageNum)
  163. ->select();
  164. if(!$list) {
  165. $this->success("背包空空如也!");
  166. }
  167. foreach($list as $k => $v) {
  168. // 有效期/剩余天数
  169. if($v["is_use"] == 1 && $v["duetime"]>time()) { // 正在使用,未过期
  170. $dtime = "剩余".ceil(($v["duetime"]-time())/86400)."天";
  171. }
  172. if($v["is_use"] == 0) {
  173. $dtime = "有效期".$v["limit_day"]."天";
  174. }
  175. $list[$k]["dtime"] = $dtime;
  176. }
  177. $this->success("获取成功!",$list);
  178. } catch (Exception $e) {echo '<pre>';var_dump($e->getMessage());exit;
  179. $this->error($e->getMessage());
  180. }
  181. }
  182. /**
  183. * 使用并装上装饰
  184. * 从GG项目 安卓代码里没搜到,没有使用到,使用的是下面的 toUsingAttire。2023.10.27
  185. */
  186. public function toUseAttire() {
  187. $id = $this->request->request("id"); //背包中商品ID
  188. if (!$id) {
  189. $this->error(__('Invalid parameters'));
  190. }
  191. // 获取装饰信息
  192. $where = [];
  193. $where["id"] = $id;
  194. $attirebackInfo = $this->attirebackModel->where($where)->find();
  195. $data = [];
  196. $data["is_use"] = 1;
  197. $data["is_using"] = 1;
  198. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  199. $res = $this->attirebackModel->update($data,$where);
  200. if($res) {
  201. $this->success("设置成功!");
  202. } else {
  203. $this->success("网络错误,请稍后重试!");
  204. }
  205. }
  206. /**
  207. * 装上
  208. */
  209. public function toUsingAttire() {
  210. $id = $this->request->request("id"); //背包中商品ID
  211. $use_type = $this->request->request("use_type",1); //使用方式:1=装上,2=卸下
  212. if (!$id || !in_array($use_type,[1,2])) {
  213. $this->error(__('Invalid parameters'));
  214. }
  215. // 获取装饰信息
  216. $where = [];
  217. $where["id"] = $id;
  218. $attirebackInfo = $this->attirebackModel->where($where)->find();
  219. if($use_type == 2) {
  220. $attirebackInfo->is_using = 0;
  221. $attirebackInfo->save();
  222. $this->success("设置成功!");
  223. }
  224. if($attirebackInfo["is_use"] != 1) {
  225. $data = [];
  226. $data["is_use"] = 1;
  227. $data["is_using"] = 1;
  228. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  229. $res = $this->attirebackModel->update($data,$where);
  230. if(!$res) $this->error("使用失败!");
  231. }
  232. Db::startTrans();
  233. try{
  234. // 先取消掉所有的
  235. $res1 = $this->attirebackModel->update(["is_using"=>0],["user_id"=>$this->auth->id,"type"=>$attirebackInfo["type"]]);
  236. // 再设置当前
  237. $res2 = $this->attirebackModel->update(["is_using"=>1],["id"=>$id]);
  238. if($res1 && $res2) {
  239. Db::commit();
  240. $this->success("设置成功!");
  241. } else {
  242. $this->success("网络错误,请稍后重试!");
  243. }
  244. }catch (ValidateException $e) {
  245. Db::rollback();
  246. $this->error($e->getMessage());
  247. } catch (PDOException $e) {
  248. Db::rollback();
  249. $this->error($e->getMessage());
  250. } catch (Exception $e) {
  251. Db::rollback();
  252. $this->error($e->getMessage());
  253. }
  254. }
  255. }