Sellervoucher.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 卖家代金券控制器
  17. */
  18. class Sellervoucher extends MobileSeller {
  19. private $quotastate_arr;
  20. private $templatestate_arr;
  21. public function initialize() {
  22. parent::initialize(); // TODO: Change the autogenerated stub
  23. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellervoucher.lang.php');
  24. if (config('ds_config.voucher_allow') != 1) {
  25. ds_json_encode(10001, lang('voucher_unavailable'));
  26. }
  27. //套餐状态
  28. $this->quotastate_arr = array(
  29. 'activity' => array(1, lang('voucher_quotastate_activity')),
  30. 'cancel' => array(2, lang('voucher_quotastate_cancel')),
  31. 'expire' => array(3, lang('voucher_quotastate_expire'))
  32. );
  33. //代金券模板状态
  34. $this->templatestate_arr = array(
  35. 'usable' => array(1, lang('voucher_templatestate_usable')),
  36. 'disabled' => array(2, lang('voucher_templatestate_disabled'))
  37. );
  38. }
  39. /**
  40. * @api {POST} api/Sellervoucher/templatelist 代金券列表
  41. * @apiVersion 1.0.0
  42. * @apiGroup Sellervoucher
  43. *
  44. * @apiHeader {String} X-DS-KEY 卖家授权token
  45. *
  46. * @apiParam {String} txt_keyword 关键词
  47. * @apiParam {String} select_state 状态 1:有效 2:失效
  48. * @apiParam {String} txt_startdate 有效期开始时间 YYYY-MM-DD
  49. * @apiParam {String} txt_enddate 有效期结束时间 YYYY-MM-DD
  50. * @apiParam {String} page 页码
  51. * @apiParam {String} pagesize 每页显示数量
  52. *
  53. * @apiSuccess {String} code 返回码,10000为成功
  54. * @apiSuccess {String} message 返回消息
  55. * @apiSuccess {Object} result 返回数据
  56. * @apiSuccess {Object} result.current_quota 代金券活动信息 (返回字段参考voucherquota)
  57. * @apiSuccess {String} result.promotion_voucher_price 购买代金劵活动所需费用
  58. * @apiSuccess {String} result.voucher_storetimes_limit 每月最多可以发布的代金劵促销活动数量
  59. * @apiSuccess {Object} result.vouchertemplate_list 代金券模板列表 (返回字段参考vouchertemplate)
  60. * @apiSuccess {Int} result.page_total 总页数
  61. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  62. */
  63. public function templatelist() {
  64. //检查过期的代金券模板状态设为失效
  65. $this->check_voucher_template_expire();
  66. $voucher_model = model('voucher');
  67. $isPlatformStore = false;
  68. $current_quota = false;
  69. if (!$this->store_info['is_platform_store']) {
  70. //查询是否存在可用套餐
  71. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  72. }
  73. //查询列表
  74. $param = array();
  75. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  76. if (trim(input('param.txt_keyword'))) {
  77. $param[] = array('vouchertemplate_title', 'like', '%' . trim(input('param.txt_keyword')) . '%');
  78. }
  79. $select_state = intval(input('param.select_state'));
  80. if ($select_state) {
  81. $param[] = array('vouchertemplate_state', '=', $select_state);
  82. }
  83. if (input('param.txt_startdate')) {
  84. $param[] = array('vouchertemplate_enddate', '>=', strtotime(input('param.txt_startdate')));
  85. }
  86. if (input('param.txt_enddate')) {
  87. $param[] = array('vouchertemplate_startdate', '<=', strtotime(input('param.txt_enddate')));
  88. }
  89. $vouchertemplate_list_ = Db::name('vouchertemplate')->where($param)->order('vouchertemplate_id desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  90. $vouchertemplate_list = $vouchertemplate_list_->items();
  91. foreach ($vouchertemplate_list as $key => $val) {
  92. if (!$val['vouchertemplate_customimg']) {
  93. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  94. } else {
  95. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] , $val['vouchertemplate_customimg']);
  96. }
  97. }
  98. $result = array_merge(array('vouchertemplate_list' => $vouchertemplate_list, 'current_quota' => $current_quota, 'promotion_voucher_price' => config('ds_config.promotion_voucher_price'), 'voucher_storetimes_limit' => config('ds_config.voucher_storetimes_limit')), mobile_page($vouchertemplate_list_));
  99. ds_json_encode(10000, '', $result);
  100. }
  101. /**
  102. * @api {POST} api/Sellervoucher/quotaadd 购买套餐
  103. * @apiVersion 1.0.0
  104. * @apiGroup Sellervoucher
  105. *
  106. * @apiHeader {String} X-DS-KEY 卖家授权token
  107. *
  108. * @apiParam {Int} quota_quantity 购买数量
  109. *
  110. * @apiSuccess {String} code 返回码,10000为成功
  111. * @apiSuccess {String} message 返回消息
  112. * @apiSuccess {Object} result 返回数据
  113. * @apiSuccess {Int} result.voucherquota_endtime 代金券活动结束时间
  114. */
  115. public function quotaadd() {
  116. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  117. ds_json_encode(10001, lang('param_error'));
  118. }
  119. $quota_quantity = intval(input('post.quota_quantity'));
  120. if ($quota_quantity <= 0 || $quota_quantity > 12) {
  121. ds_json_encode(10001, lang('voucher_apply_num_error'));
  122. }
  123. //获取当前价格
  124. $current_price = intval(config('ds_config.promotion_voucher_price'));
  125. $voucher_model = model('voucher');
  126. //获取该用户已有套餐
  127. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  128. $quota_add_time = 86400 * 30 * $quota_quantity;
  129. if (empty($current_quota)) {
  130. //生成套餐
  131. $param = array();
  132. $param['voucherquota_memberid'] = $this->seller_info['member_id'];
  133. $param['voucherquota_membername'] = $this->seller_info['seller_name'];
  134. $param['voucherquota_storeid'] = $this->store_info['store_id'];
  135. $param['voucherquota_storename'] = $this->store_info['store_name'];
  136. $param['voucherquota_starttime'] = TIMESTAMP;
  137. $param['voucherquota_endtime'] = TIMESTAMP + $quota_add_time;
  138. $param['voucherquota_state'] = 1;
  139. $reault = Db::name('voucherquota')->insert($param);
  140. } else {
  141. $param = array();
  142. $param['voucherquota_endtime'] = $current_quota['voucherquota_endtime'] + $quota_add_time;
  143. $reault = Db::name('voucherquota')->where(array('voucherquota_id' => $current_quota['voucherquota_id']))->update($param);
  144. }
  145. //记录店铺费用
  146. $this->recordStorecost($current_price * $quota_quantity, lang('buy_voucher_package'));
  147. $this->recordSellerlog(lang('buy') . $quota_quantity . lang('voucher_plan') . $current_price . lang('ds_yuan'));
  148. if ($reault) {
  149. ds_json_encode(10000, lang('voucher_apply_buy_succ'), array('voucherquota_endtime' => $param['voucherquota_endtime']));
  150. } else {
  151. ds_json_encode(10001, lang('ds_common_op_fail'));
  152. }
  153. }
  154. /**
  155. * @api {POST} api/Sellervoucher/templateadd 代金券模版添加
  156. * @apiVersion 1.0.0
  157. * @apiGroup Sellervoucher
  158. *
  159. * @apiHeader {String} X-DS-KEY 卖家授权token
  160. *
  161. * @apiParam {String} txt_template_title 代金券模版名称
  162. * @apiParam {String} txt_template_total 代金券总数
  163. * @apiParam {String} select_template_price 面额
  164. * @apiParam {String} txt_template_limit 订单限额
  165. * @apiParam {String} txt_template_describe 代金券模版描述
  166. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  167. * @apiParam {String} storeclass_id 所属店铺分类ID
  168. * @apiParam {String} eachlimit 每人限领张数
  169. *
  170. * @apiSuccess {String} code 返回码,10000为成功
  171. * @apiSuccess {String} message 返回消息
  172. * @apiSuccess {Object} result 返回数据
  173. */
  174. public function templateadd() {
  175. $voucher_model = model('voucher');
  176. $isPlatformStore = $this->store_info['is_platform_store'];
  177. $quotainfo = array();
  178. if (!$isPlatformStore) {
  179. //查询当前可以使用的套餐
  180. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  181. if (empty($quotainfo)) {
  182. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  183. $quotainfo = array('voucherquota_id' => 0, 'voucherquota_starttime' => TIMESTAMP, 'voucherquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  184. } else {
  185. ds_json_encode(10001, lang('voucher_template_quotanull'));
  186. }
  187. }
  188. //查询该套餐下代金券模板列表
  189. $count = Db::name('vouchertemplate')->where(array('vouchertemplate_quotaid' => $quotainfo['voucherquota_id'], 'vouchertemplate_state' => $this->templatestate_arr['usable'][0]))->count();
  190. if ($count >= config('ds_config.voucher_storetimes_limit')) {
  191. $message = sprintf(lang('voucher_template_noresidual'), config('ds_config.voucher_storetimes_limit'));
  192. ds_json_encode(10001, $message);
  193. }
  194. }
  195. $common_data = $this->common_data();
  196. $pricelist = $common_data['pricelist'];
  197. //验证提交的内容面额不能大于限额
  198. $data = [
  199. 'txt_template_title' => input('post.txt_template_title'),
  200. 'txt_template_total' => input('post.txt_template_total'),
  201. 'select_template_price' => input('post.select_template_price'),
  202. 'txt_template_limit' => input('post.txt_template_limit'),
  203. 'txt_template_describe' => input('post.txt_template_describe'),
  204. ];
  205. $sellervoucher_validate = ds_validate('sellervoucher');
  206. if (!$sellervoucher_validate->scene('templateadd')->check($data)) {
  207. ds_json_encode(10001, $sellervoucher_validate->getError());
  208. }
  209. //金额验证
  210. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  211. foreach ($pricelist as $k => $v) {
  212. if ($v['voucherprice'] == $price) {
  213. $chooseprice = $v; //取得当前选择的面额记录
  214. }
  215. }
  216. if (empty($chooseprice)) {
  217. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  218. }
  219. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  220. if ($price >= $limit) {
  221. ds_json_encode(10001, lang('voucher_template_price_error'));
  222. }
  223. $insert_arr = array();
  224. $insert_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  225. $insert_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  226. $insert_arr['vouchertemplate_startdate'] = TIMESTAMP; //默认代金券模板的有效期为当前时间
  227. if (input('post.txt_template_enddate')) {
  228. $enddate = strtotime(input('post.txt_template_enddate'));
  229. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  230. $enddate = $quotainfo['voucherquota_endtime'];
  231. }
  232. $insert_arr['vouchertemplate_enddate'] = $enddate;
  233. } else {//如果没有添加有效期则默认为套餐的结束时间
  234. if ($isPlatformStore)
  235. $insert_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  236. else
  237. $insert_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  238. }
  239. $insert_arr['vouchertemplate_price'] = $price;
  240. $insert_arr['vouchertemplate_limit'] = $limit;
  241. $insert_arr['vouchertemplate_store_id'] = $this->store_info['store_id'];
  242. $insert_arr['vouchertemplate_storename'] = $this->store_info['store_name'];
  243. $insert_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  244. $insert_arr['vouchertemplate_creator_id'] = $this->seller_info['member_id'];
  245. $insert_arr['vouchertemplate_state'] = $this->templatestate_arr['usable'][0];
  246. $insert_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  247. $insert_arr['vouchertemplate_giveout'] = 0;
  248. $insert_arr['vouchertemplate_used'] = 0;
  249. $insert_arr['vouchertemplate_gettype'] = 1;
  250. $insert_arr['vouchertemplate_adddate'] = TIMESTAMP;
  251. $insert_arr['vouchertemplate_quotaid'] = isset($quotainfo['voucherquota_id']) ? $quotainfo['voucherquota_id'] : 0;
  252. $insert_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  253. $insert_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  254. $insert_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  255. //自定义图片
  256. if (!empty($_FILES['customimg']['name'])) {
  257. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  258. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  259. if ($res['code']) {
  260. $file_name = $res['data']['file_name'];
  261. $insert_arr['vouchertemplate_customimg'] = $file_name;
  262. } else {
  263. ds_json_encode(10001, $res['msg']);
  264. }
  265. }
  266. $rs = Db::name('vouchertemplate')->insert($insert_arr);
  267. if ($rs) {
  268. ds_json_encode(10000, lang('ds_common_op_succ'));
  269. } else {
  270. ds_json_encode(10001, lang('ds_common_op_fail'));
  271. }
  272. }
  273. public function common_data() {
  274. $voucher_model = model('voucher');
  275. //查询面额列表
  276. $pricelist = Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
  277. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  278. $quotainfo['voucherquota_endtime'] = date('Y-m-d',$quotainfo['voucherquota_endtime']);
  279. if (empty($pricelist)) {
  280. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  281. }
  282. return array('pricelist' => $pricelist,'quotainfo'=>$quotainfo, 'voucher_buyertimes_limit' => config('ds_config.voucher_buyertimes_limit'));
  283. }
  284. /**
  285. * @api {POST} api/Sellervoucher/get_common_data 代金券新增/编辑公共数据
  286. * @apiVersion 1.0.0
  287. * @apiGroup Sellervoucher
  288. *
  289. * @apiHeader {String} X-DS-KEY 卖家授权token
  290. *
  291. * @apiSuccess {String} code 返回码,10000为成功
  292. * @apiSuccess {String} message 返回消息
  293. * @apiSuccess {Object} result 返回数据
  294. * @apiSuccess {Object[]} result.pricelist 面额列表
  295. * @apiSuccess {Object[]} result.pricelist.voucherprice 面额
  296. * @apiSuccess {Object[]} result.pricelist.voucherprice_defaultpoints 兑换积分
  297. * @apiSuccess {Object[]} result.pricelist.voucherprice_describe 描述
  298. * @apiSuccess {Object[]} result.pricelist.voucherprice_id 面额ID
  299. * @apiSuccess {String} result.voucher_buyertimes_limit 买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量
  300. */
  301. public function get_common_data() {
  302. $common_data = $this->common_data();
  303. ds_json_encode(10000, '', $common_data);
  304. }
  305. /**
  306. * @api {POST} api/Sellervoucher/templateadd 代金券模版编辑
  307. * @apiVersion 1.0.0
  308. * @apiGroup Sellervoucher
  309. *
  310. * @apiHeader {String} X-DS-KEY 卖家授权token
  311. *
  312. * @apiParam {Int} tid 代金券模版ID
  313. * @apiParam {String} txt_template_title 代金券模版名称
  314. * @apiParam {String} txt_template_total 代金券总数
  315. * @apiParam {String} select_template_price 面额
  316. * @apiParam {String} txt_template_limit 订单限额
  317. * @apiParam {String} txt_template_describe 代金券模版描述
  318. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  319. * @apiParam {String} storeclass_id 所属店铺分类ID
  320. * @apiParam {String} eachlimit 每人限领张数
  321. * @apiParam {Int} tstate 代金券模版状态 1:有效 2:失效
  322. *
  323. * @apiSuccess {String} code 返回码,10000为成功
  324. * @apiSuccess {String} message 返回消息
  325. * @apiSuccess {Object} result 返回数据
  326. */
  327. public function templateedit() {
  328. $t_id = intval(input('param.tid'));
  329. if ($t_id <= 0) {
  330. ds_json_encode(10001, lang('param_error'));
  331. }
  332. //查询模板信息
  333. $param = array();
  334. $param[] = array('vouchertemplate_id', '=', $t_id);
  335. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  336. $param[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  337. $param[] = array('vouchertemplate_giveout', '<=', '0');
  338. $param[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  339. $t_info = Db::name('vouchertemplate')->where($param)->find();
  340. if (empty($t_info)) {
  341. ds_json_encode(10001, lang('param_error'));
  342. }
  343. $isPlatformStore = $this->store_info['is_platform_store'];
  344. View::assign('isPlatformStore', $isPlatformStore);
  345. $quotainfo = array();
  346. if (!$isPlatformStore) {
  347. //查询套餐信息
  348. $quotainfo = Db::name('voucherquota')->where(array(
  349. 'voucherquota_id' => $t_info['vouchertemplate_quotaid'],
  350. 'voucherquota_storeid' => $this->store_info['store_id']
  351. ))->find();
  352. if (empty($quotainfo)) {
  353. ds_json_encode(10001, lang('voucher_template_quotanull'));
  354. }
  355. }
  356. $common_data = $this->common_data();
  357. $pricelist = $common_data['pricelist'];
  358. //验证提交的内容面额不能大于限额
  359. $data = [
  360. 'txt_template_title' => input('post.txt_template_title'),
  361. 'txt_template_total' => input('post.txt_template_total'),
  362. 'select_template_price' => input('post.select_template_price'),
  363. 'txt_template_limit' => input('post.txt_template_limit'),
  364. 'txt_template_describe' => input('post.txt_template_describe'),
  365. ];
  366. $sellervoucher_validate = ds_validate('sellervoucher');
  367. if (!$sellervoucher_validate->scene('templateedit')->check($data)) {
  368. ds_json_encode(10001, $sellervoucher_validate->getError());
  369. }
  370. //金额验证
  371. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  372. foreach ($pricelist as $k => $v) {
  373. if ($v['voucherprice'] == $price) {
  374. $chooseprice = $v; //取得当前选择的面额记录
  375. }
  376. }
  377. if (empty($chooseprice)) {
  378. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  379. }
  380. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  381. if ($price >= $limit) {
  382. ds_json_encode(10001, lang('voucher_template_price_error'));
  383. }
  384. $update_arr = array();
  385. $update_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  386. $update_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  387. if (input('post.txt_template_enddate')) {
  388. $enddate = strtotime(input('post.txt_template_enddate'));
  389. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  390. $enddate = $quotainfo['voucherquota_endtime'];
  391. }
  392. $update_arr['vouchertemplate_enddate'] = $enddate;
  393. } else {//如果没有添加有效期则默认为套餐的结束时间
  394. if ($isPlatformStore)
  395. $update_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  396. else
  397. $update_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  398. }
  399. $update_arr['vouchertemplate_price'] = $price;
  400. $update_arr['vouchertemplate_limit'] = $limit;
  401. $update_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  402. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  403. $update_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  404. $update_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  405. $update_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  406. $update_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  407. //自定义图片
  408. if (!empty($_FILES['customimg']['name'])) {
  409. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999);
  410. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  411. if ($res['code']) {
  412. $file_name = $res['data']['file_name'];
  413. //删除原图
  414. if (!empty($t_info['vouchertemplate_customimg'])) {//如果模板存在,则删除原模板图片
  415. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  416. }
  417. $update_arr['vouchertemplate_customimg'] = $file_name;
  418. } else {
  419. ds_json_encode(10001, $res['msg']);
  420. }
  421. }
  422. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->update($update_arr);
  423. if ($rs) {
  424. ds_json_encode(10000, lang('ds_common_op_succ'));
  425. } else {
  426. ds_json_encode(10001, lang('ds_common_op_fail'));
  427. }
  428. }
  429. /**
  430. * @api {POST} api/Sellervoucher/templatedel 删除代金券
  431. * @apiVersion 1.0.0
  432. * @apiGroup Sellervoucher
  433. *
  434. * @apiHeader {String} X-DS-KEY 卖家授权token
  435. *
  436. * @apiParam {Int} tid 代金券模版ID
  437. *
  438. * @apiSuccess {String} code 返回码,10000为成功
  439. * @apiSuccess {String} message 返回消息
  440. * @apiSuccess {Object} result 返回数据
  441. */
  442. public function templatedel() {
  443. $t_id = intval(input('param.tid'));
  444. if ($t_id <= 0) {
  445. ds_json_encode(10001, lang('param_error'));
  446. }
  447. //查询模板信息
  448. $param = array();
  449. $param[] = array('vouchertemplate_id', '=', $t_id);
  450. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  451. $param[] = array('vouchertemplate_giveout', '<=', '0'); //会员没领取过代金券才可删除
  452. $t_info = Db::name('vouchertemplate')->where($param)->find();
  453. if (empty($t_info)) {
  454. ds_json_encode(10001, lang('param_error'));
  455. }
  456. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->delete();
  457. if ($rs) {
  458. //删除自定义的图片
  459. if (trim($t_info['vouchertemplate_customimg'])) {
  460. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  461. }
  462. ds_json_encode(10000, lang('ds_common_del_succ'));
  463. } else {
  464. ds_json_encode(10001, lang('ds_common_del_fail'));
  465. }
  466. }
  467. /**
  468. * 查看代金券详细
  469. */
  470. public function templateinfo() {
  471. $t_id = intval(input('param.tid'));
  472. if ($t_id <= 0) {
  473. ds_json_encode(10001, lang('param_error'), 'Sellervoucher/templatelist');
  474. }
  475. //查询模板信息
  476. $param = array();
  477. $param['vouchertemplate_id'] = $t_id;
  478. $param['vouchertemplate_store_id'] = $this->store_info['store_id'];
  479. $t_info = Db::name('vouchertemplate')->where($param)->find();
  480. ds_json_encode(10000, lang('ds_common_del_succ'), array('t_info' => $t_info));
  481. }
  482. /*
  483. * 把代金券模版设为失效
  484. */
  485. private function check_voucher_template_expire($voucher_template_id = '') {
  486. $where_array = array();
  487. if (empty($voucher_template_id)) {
  488. $where_array[] = array('vouchertemplate_enddate', '<', TIMESTAMP);
  489. } else {
  490. $where_array[] = array('vouchertemplate_id', '=', $voucher_template_id);
  491. }
  492. $where_array[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  493. Db::name('vouchertemplate')->where($where_array)->update(array('vouchertemplate_state' => $this->templatestate_arr['disabled'][0]));
  494. }
  495. }